ASP.NET MVC Routes: How do I omit "index" from a URL

I have a controller called “StuffController” with a parameterless Index action. I want this action to be called from a URL in the form mysite.com/stuff

My controller is defined as

public class StuffController : BaseController
{
    public ActionResult Index()
    {
        // Return list of Stuff
    }
}

I added a custom route so the routes are defined like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Custom route to show index
    routes.MapRoute(
        name: "StuffList",
        url: "Stuff",
        defaults: new { controller = "Stuff", action = "Index" }
    );


    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

But when I try to browse to mysite.com/stuff I get an error

HTTP Error 403.14 – Forbidden

The Web server is configured to not list the contents of this directory.

The URL mysite.com/stuff/index works fine. What I am doing wrong?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

HTTP Error 403.14 – Forbidden The Web server is configured to not list the contents of this directory.

The error indicates that you have a virtual directory (probably a physical one) in your project called /Stuff. By default, IIS will first reach this directory and look for a default page (for example /index.html), and if no default page exists will attempt to list the contents of the directory (which requires a configuration setting).

This all happens before IIS passes the call to .NET routing, so having a directory with the name /Stuff is causing your application not to function correctly. You need to either delete the directory named /Stuff or use a different name for your route.

And as others have mentioned, the default route covers this scenario so there is no need for a custom route in this case.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Passing the URL `/Stuff` will match this route and cause it
    // to look for a controller named `StuffController` with action named `Index`.
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Solution 2

It seems that your scenario is covered fine by default route, so there is no need for a custom Stuff one.

As to why the error is thrown, the fact that action is listed in defaults does not mean that it is actually becoming a part of a route. It should be mentioned in the route, otherwise it appears as there is no action at all. So what I think happens here is that first route is matched, but it cannot be processed as there is no action specified, so MVC passes request on to IIS, which throws the named error.

The fix would be simple:

// Custom route to show index
routes.MapRoute(
    name: "StuffList",
    url: "Stuff/{action}",
    defaults: new { controller = "Stuff", action = "Index" }
);

But again, you shouldn’t need that at all.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply