Can Asp.Net Mvc Route Constraints throw 404 instead of InvalidOperationException?

I am trying to use route restrictions in an ASP.NET MVC application.

routes.MapRoute(
    "theRoute",
    "MyAction/{page}",
    new { controller = "TheController", action = "MyAction", page = 1 },
    new { page = @"[0-9]" });

When I enter a URL like ~ / MyAction / aString, YSOD is displayed with an invalid operation exception. What can I do to redirect the wrong url to page 404?

I know that I can solve the problem using a string parameter in the action of the controller and int.TryParse, but then the route constant is useless.

How can I choose an exception that is given by route restrictions?

+3
source share
2 answers

The problem is that you do not have a route that matches a route that ends in a line.

, :

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = 0 },
    new { id = "[0-9]" }// Parameter defaults
);
routes.MapRoute(
    "Default2",                                              // Route name
    "{controller}/{action2}/{sid}",                           // URL with parameters
    new { controller = "Home", action = "Index2", sid = "" }  // Parameter defaults
);

public ActionResult Index(int id)
    {
        ViewData["Title"] = "Home Page";
        ViewData["Message"] = "Welcome to ASP.NET MVC! Your id is: "+ id.ToString();

        return View();
    }

    public ActionResult Index2(string sid)
    {
        ViewData["Title"] = "Home Page 2."+sid.ToString();
        ViewData["Message"] = "Welcome to ASP.NET MVC! \"" + sid.ToString() +"\" is an invalid id";

        return View("index");
    }

, , Index2, , , .

+3

:

Web.config :

<system.web>
    ...
    ...
    <customErrors mode="On">
        <error 
            statusCode="404" 
            redirect="/Home/MyCustomError" /> 
                                <!--    Is not necessary that the 
                                        view MyCustomError.aspx are inside the 
                                        Home folder, you can put that 
                                        view in the Shared folder.
                                -->
    </customErrors>
    ...
    ...
</system.web>

ActionResult, MyCustomError

public class HomeController : Controller
{
    ...
    ...

    public ActionResult MyCustomError(string aspxerrorpath) 
                                                    /* the var aspxerrorpath 
                                                     * is that MVC generated by
                                                     * default */
    {
        ViewData["messageError"] = aspxerrorpath;
        return View();
    }
}

:

<%@ Page Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Error
</asp:Content>

<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Shit happends</h2>
     <p> <%: ViewData["messageError"]%></p>
     <p>aaaaaaaaaaaaaaa!!!!!!!!!!!!!!!!!!!!</p>
</asp:Content>
0

All Articles