Tengo un problema al crear una vista genérica para representar las páginas NotFound.Crear una vista NotFound genérica en ASP.MVC
La vista está creada y está bien. Necesito saber cómo puedo dirigir al usuario a la vista NotFound en mis controladores y cómo renderizar un "retorno al índice" específico en cada controlador.
Aquí hay un código:
public class NotFoundModel
{
private string _contentName;
private string _notFoundTitle;
private string _apologiesMessage;
public string ContentName { get; private set; }
public string NotFoundTitle { get; private set; }
public string ApologiesMessage { get; private set; }
public NotFoundModel(string contentName, string notFoundTitle, string apologiesMessage)
{
this._contentName = contentName;
this._notFoundTitle = notFoundTitle;
this._apologiesMessage = apologiesMessage;
}
}
// NotFound Ver
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Geographika.Models.NotFoundModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%= Html.Encode(Model.ContentName) %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(Model.NotFoundTitle) %></h2>
<p><%= Html.Encode(Model.ApologiesMessage) %></p>
<!-- How can i render here a specific "BackToIndexView", but that it's not bound to
my NotFoundModel? -->
</asp:Content>
// Controlador pieza de código
//
// GET: /Term/Details/2
public ActionResult Details(int id)
{
Term term = termRepository.SingleOrDefault(t => t.TermId == id);
if (term == null)
return View("NotFound"); // how can i return the specific view that its not bound to Term Model?
// the idea here would be something like:
// return View("NotFound",new NotFoundModel("a","b","c"));
else
return View("Details", term);
}
No estoy seguro de cómo redirigir a una página completamente diferente. ¿Alguien puede darme algún consejo?
Gracias
Genial, muy simple de hecho! –