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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With