Creating a generic NotFound View in ASP.MVC


Creating a generic NotFound View in ASP.MVC



I'm having a problem to create a generic View to represent NotFound pages.

The view is created and it's fine. I need to know how i can direct the user to the NotFound view in my Controllers and how to render a specific "Return to Index" in each controller.

Here is some code:

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> 

// Controller piece of code

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

I'm not sure how to redirect to a whole different page. Can anyone give me any pointers?

Thanks


Is ADO.NET Entity Framework (with ASP.NET MVC v2) a viable option when writing custom and contantly updated websites?

1:

How to disable VS compile warning “class or css class is not defined”
Very simple, this is what I use and has very little dependencies.. patterns to use when building complex web UI Create an ErrorController.cs in Controllers:. Validation not firing even when my form is obviously invalid
public class ErrorController : Controller     {         public ErrorController()         {             //_logger = logger; // log here if you had a logger!         }          /// <summary>         /// This is fired when the site receive s a bad URL         /// </summary>         /// <returns></returns>         public ActionResult NotFound()         {             // log here, perhaps you want to know when a user reaches a 404?             return View();         }     } } 
Then simply create a Views\Error\NotFound.aspx with the following contents, tweak as you feel fit (including your "Back to home" link, I'll include a default one for you):. asp.net mvc datannotation password validate
<%@ 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 this page</h2>      <p>Sorry, i cannot find the page you are looking for</p>  </asp:Content> 
Then simply in your MVC app Web.config within the <system.web> tags:. ASP.NET MVC - How controls retain their state
<customErrors mode="Off" defaultRedirect="/error/problem">     <error statusCode="404" redirect="/error/notfound"/> </customErrors> 
No custom route required either if you use the standard catch-all route. JavaScript url auto-resolution in Asp.Net MVCHope this helps.. How Testing relates to ASP.NET MVC/WEBFORMS Silverlight MVVM

2:

thank you for your input. Thinking hard here, I managed to create one single NotFound view and model like this:.
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;     }      } 
My 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>      <%= Html.ActionLink(Model.LinkText,Model.Action,Model.Controller) %>  </asp:Content> 
and this is a case of how I'm using it:.
    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");     } 
Somehow ASP.MVC searched for all NotFound views in shared folders too, so being the only one, it renders this one with a link to an appropriate "Go to Model index" link.. Thanks for all the help..


67 out of 100 based on 42 user ratings 277 reviews