C# .net Core MapRoute Not Working?

Recently I faced a challenge while trying to programatically register routes on a .net core MVC application. The following was my code.

app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute(
name: "example-route",
template: "example",
defaults: new { controller = "ExampleController", action = "ExampleAction" }
);
});

Once the route was registered, I tried navigating to it and I kept getting a 404. Was breaking my head as to why this was happening. Finally found the issue.

The controller name cannot have the post-fix Controller, the following change made the trick.

app.UseMvc(routeBuilder =>
{
routeBuilder.MapRoute(
name: "example-route",
template: "example",
defaults: new { controller = "ExampleController", action = "ExampleAction" }
);
});

Hope this helps someone, break their head over this trivial thing.

Cheers!


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.