Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in ASP.NET MVC 3 the default route is not working for a controller called "ContentController"?

I have a brand new asp.net mvc 3 project. I did not modify the routes in any way. I have a controller called PageController and another controller call ContentController.

When I browse to domain.com/Page then the Index action on the Page controller gets executed as expected and displays the Index view.

When I browse to domain.com/Content I get a 404 error. If I browse to domain.com/Content/Index then it works fine.

How do I troubleshoot this single route?

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        ); 
    }

I tried adding an additional route:

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        ); 
        routes.MapRoute(
             "Content", // Route name
             "Content/{action}/{id}", // URL with parameters
             new { controller = "Content", action = "Index", id = UrlParameter.Optional } // Parameter defaults
         );

    }

But the additional route did not change the application's behavior.

What could be causing this?

like image 868
quakkels Avatar asked Sep 28 '11 15:09

quakkels


People also ask

What is default route in ASP NET MVC?

ASP.NET MVC - Routing. So this route that is defined in the application is the default route. As seen in the above code, when you see a URL arrive in the form of (something)/ (something)/ (something), then the first piece is the controller name, second piece is the action name, and the third piece is an ID parameter.

What is the default file for an ASP NET MVC application?

The file in Listing 1 contains the default Global.asax file for an ASP.NET MVC application. Listing 1 - Global.asax.cs When an MVC application first starts, the Application_Start () method is called.

How does MVC work in ASP NET?

Asp.net mvc automatically mapes request data to parameter values for action methods. If an action method takes a parameter, mvc framework looks for a parameter of the same name in the request data.

What is routing in ASP NET Core MVC?

ASP.NET Core MVC uses the Routing middleware to match the URLs of incoming requests and map them to actions. Routes are defined in startup code or attributes. Routes describe how URL paths should be matched to actions. Routes are also used to generate URLs (for links) sent out in responses.


1 Answers

its because there is a physical folder called content. having a controller with the same name as a physical folder will probably have some adverse affects on your website.

like image 137
nathan gonzalez Avatar answered Oct 25 '22 01:10

nathan gonzalez