我有一个问题是创建一个通用视图来表示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;
}
}
Run Code Online (Sandbox Code Playgroud)
// 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>
Run Code Online (Sandbox Code Playgroud)
//控制器代码片段
//
// 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);
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何重定向到一个完整的不同页面.任何人都可以给我任何指示吗?
谢谢
很简单,这就是我使用的并且几乎没有依赖性。
在控制器中创建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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需创建一个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>
Run Code Online (Sandbox Code Playgroud)
然后只需在您的 MVC 应用程序 Web.config 中的<system.web>标签内:
<customErrors mode="Off" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)
如果您使用标准的包罗万象的路由,则不需要自定义路由。希望有帮助。
| 归档时间: |
|
| 查看次数: |
1018 次 |
| 最近记录: |