2010-05-20 28 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視圖,因此它是唯一的一個,它通過指向適當的「轉到模型索引」鏈接的鏈接來呈現該鏈接。

感謝您的幫助。