簡體   English   中英

在ASP.MVC中創建通用NotFound視圖

[英]Creating a generic NotFound View in 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 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>

//控制器代碼片段

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

我不知道如何重定向到一個完整的不同頁面。 任何人都可以給我任何指示嗎?

謝謝

非常簡單,這是我使用的,並且具有很少的依賴性。

在控制器中創建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>

然后只需在<system.web>標記內的MVC應用程序Web.config中:

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

如果您使用標准的全能路線,則無需自定義路線。 希望有所幫助。

謝謝您的意見。 在這里努力思考,我設法創建了一個單一的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視圖,因此作為唯一的視圖,它會將此鏈接呈現為指向相應“轉到模型索引”鏈接的鏈接。

謝謝你的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM