C# 在ASP.MVC中创建通用NotFound视图
我在创建表示未找到页面的通用视图时遇到问题 视图已创建,一切正常。我需要知道如何将用户引导到控制器中的NotFound视图,以及如何在每个控制器中呈现特定的“返回索引” 下面是一些代码:C# 在ASP.MVC中创建通用NotFound视图,c#,asp.net-mvc,C#,Asp.net Mvc,我在创建表示未找到页面的通用视图时遇到问题 视图已创建,一切正常。我需要知道如何将用户引导到控制器中的NotFound视图,以及如何在每个控制器中呈现特定的“返回索引” 下面是一些代码: public class NotFoundModel { private string _contentName; private string _notFoundTitle; private string _apologiesMessage; public string Co
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;
}
}
//未找到视图
<%@ 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>
我不知道如何重定向到一个完全不同的页面。有人能给我指点吗
谢谢你的意见。在这里仔细思考,我成功地创建了一个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>
不知何故,ASP.MVC也搜索了共享文件夹中所有未找到的视图,因此它是唯一的一个视图,它使用指向适当“转到模型索引”链接的链接呈现此视图
感谢所有的帮助。非常简单,这就是我使用的,并且没有太多依赖性 在控制器中创建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();
}
}
}
然后只需在MVC应用程序Web.config中的
标记中:
<customErrors mode="Off" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>
如果使用标准的“全部捕获”路线,也不需要自定义路线。希望有帮助
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();
}
}
}
<%@ 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>
<customErrors mode="Off" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>