我在创建通用视图以表示NotFound页面时遇到问题。
视图已创建且没问题。我需要知道如何将用户引导到控制器中的NotFound视图以及如何在每个控制器中呈现特定的“返回索引”。
以下是一些代码:
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 View
<%@ 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>
//控制器代码
//
// 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);
}
我不确定如何重定向到整个不同的页面。任何人都可以给我任何指示吗?
由于
答案 0 :(得分:4)
很简单,这是我使用的,并且几乎没有依赖。
在控制器中创建一个ErrorController.cs:
public class ErrorController : Controller
{
public ErrorController()
{
//_logger = logger; // log here if you had a logger!
}
/// <summary>
/// This is fired when the site gets a bad URL
/// </summary>
/// <returns></returns>
public ActionResult NotFound()
{
// log here, perhaps you want to know when a user reaches a 404?
return View();
}
}
}
然后只需创建一个包含以下内容的Views\Error\NotFound.aspx
,根据您的需要进行调整(包括“返回主页”链接,我将为您提供默认选项):
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Oops - No content here!
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>404 Error - Can't find that page</h2>
<p>Sorry, we cannot find the page you are looking for</p>
</asp:Content>
然后只需在<system.web>
标签中的MVC应用Web.config中:
<customErrors mode="Off" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>
如果您使用标准的全能路线,则无需自定义路线。希望有所帮助。
答案 1 :(得分:0)
感谢您的投入。在这里努力思考,我设法创建了一个单一的NotFound视图和这样的模型:
public class NotFoundModel
{
private string _contentName;
private string _notFoundTitle;
private string _apologiesMessage;
private string _linkText;
private string _action;
private string _controller;
// properties omitted for brevity;
public NotFoundModel(string contentName, string notFoundTitle, string apologiesMessage,
string linkText, string action, string controller)
{
this._contentName = contentName;
this._notFoundTitle = notFoundTitle;
this._apologiesMessage = apologiesMessage;
this._linkText = linkText;
this._action = action;
this._controller = controller;
}
}
我的观点
<%@ 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>
<%= Html.ActionLink(Model.LinkText,Model.Action,Model.Controller) %>
</asp:Content>
这是我如何使用它的一个例子:
public ActionResult Delete(int id)
{
Term term = termRepository.SingleOrDefault(t => t.TermId == id);
if (term == null)
return View("NotFound", new NotFoundModel("Termo não encontrado", "Termo não encontrado",
"Nos desculpe, mas não conseguimos encontrar o termo solicitado.", "Indíce de Termos", "Index", "Term"));
else
return View("Delete");
}
不知何故,ASP.MVC也搜索共享文件夹中的所有NotFound视图,因此作为唯一的视图,它会使用指向相应“转到模型索引”链接的链接呈现此视图。
感谢您的帮助。