2010-05-20 22 views
5

NotFound sayfalarını temsil eden genel bir Görünüm oluşturmak için bir sorun yaşıyorum.ASP.MVC'de genel NotFound Görünümü oluşturma

Görünüm oluşturuldu ve sorun değil. Denetleyicilerimde kullanıcıyı NotFound görünümüne nasıl yönlendirebileceğimi ve her denetleyicide belirli bir "Dizine Geri Dön" işleminin nasıl yapıldığını bilmem gerekiyor. İşte

bazı kod şudur:

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 Görünüm

<%@ 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> 

// Denetleyici kodu

// 
    // 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); 
    } 

parçası bir yönlendirme nasıl emin değilim tüm farklı sayfa. Bana herhangi bir işaretçi verebilir mi?

Teşekkürler

cevap

4

Çok basit, kullanıyorum ve çok az bağımlılık var.

Kontrolörleri bir ErrorController.cs oluşturun:

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(); 
     } 
    } 
} 

Sonra basitçe aşağıdaki içeriğe sahip bir Views\Error\NotFound.aspx oluşturmak, size "Geri eve" bağlantısını içeren (uygun hissetmek olarak ince ayar, bir varsayılan ekleriz sizin için bir tane): <system.web> etiketleri içinde sizin MVC uygulaması Web.config, basitçe Sonra

<%@ 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> 

Standart yakalama yolunu kullanırsanız, özel bir rota gerekmez. Umarım yardımcı olur.

+0

Gerçekten harika, çok basit! –

0

Girişiniz için teşekkürler. Burada sert düşünerek, böyle tek NotFound görünümü ve modelini oluşturmak için yönetilen:

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; 
    } 

    } 

Benim görünümü

<%@ 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> 

ve bunu bu şekilde kullanıyorum nasıl bir örnektir:

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"); 
    } 

Bir şekilde ASP.MVC, paylaşılan klasörlerdeki tüm NotFound görünümlerini de aradı, bu yüzden tek olan, bu, uygun bir "Model dizine git" bağlantısına sahip bir bağlantı oluşturur.

Yardım için teşekkürler.

İlgili konular