2010-05-20 102 views
5

我在创建通用视图来表示NotFound页面时遇到问题。在ASP.MVC中创建通用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查看

<%@ 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); 
    } 

我不知道如何重定向到一个完全不同的页面。任何人都可以给我任何指针?

谢谢

回答

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> 

然后,只需在你的MVC应用程序Web.config中<system.web>标签内:

<customErrors mode="Off" defaultRedirect="/error/problem"> 
    <error statusCode="404" redirect="/error/notfound"/> 
</customErrors> 

如果您使用标准的捕获全部路由,则不需要自定义路由。希望有所帮助。

+0

很酷,很简单! – 2010-05-21 12:56:34

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视图,因此它是唯一的一个,它通过指向适当的“转到模型索引”链接的链接来呈现该链接。

感谢您的帮助。