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

I'm trying to use route constraints in an Asp.Net MVC Application.

    new { controller = "TheController", action = "MyAction", page = 1 },
    new { page = @"[0-9]" });

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

I know I can solve the issue with a string parameter in the controller action and int.TryParse, but then the route constaint is useless.

How can I choose the exceptiontype that is thrown by the route constraints?


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

Modify your routes similar to:

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

and modify your controller

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

now when you pass a string for the ID, Index2 will be called and you can do whatever you need to do to handle the incorrect parameter.

Just to mention a more general redirection:

You can write in the Web.config of your application:

    <customErrors mode="On">
            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.

Then you need to have an ActionResult called MyCustomError

public class HomeController : Controller

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

Then you can make a custom error page:

<%@ Page Language="C#" 
        Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

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

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

Need Your Help

Display HTML file in uiwebview from SQLite database?

iphone html ios sqlite uiwebview

I have a project that I am working on that requires me to be able to display an HTML file stored in a SQLite database in a uiwebview. I know how to display a local file stored in the main bundle, b...

Oracle SQL using Like for a wildcard

sql oracle select where sql-like

I know you can use LIKE for a select statement to search for a string and then use wildcards to match that string.. ex: