Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net Core - Default controller is not loading when Route attribute is used

A new .net core web application project comes with the following route configuration:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

If you replace this with app.UseMvc() and add appropriate Route attributes for both HomeController and its actions (Index, About, Contact, Error), it would still work. Since we're not specifying a default route, the default view (Home/Index) will not be rendered if you hit http://localhost:25137/. Hope that understanding is correct!

Now, since I need the default view to be shown when the http://localhost:25137/ is hit, I changed the routing code to app.UseMvcWithDefaultRoute(); which by definition will do the equivalent to the initial snippet. Even then, it was not rendering the default view; but worked when used the complete URL(http://localhost:25137/home/index). That means the routing still works but not the default one!

Then I went back to the controller and removed all the Route attribute from the HomeController and its actions. Then the default routing worked with out any issues.

Is that the expected behavior? What could be the reason behind this behavior?

like image 999
Mahesh V S Avatar asked Oct 05 '16 13:10

Mahesh V S


People also ask

How do I set the default route in .NET Core Web API?

Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

How can use route in MVC controller?

Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .


1 Answers

From the asp docs:

Actions are either conventionally routed or attribute routed. Placing a route on the controller or the action makes it attribute routed. Actions that define attribute routes cannot be reached through the conventional routes and vice-versa. Any route attribute on the controller makes all actions in the controller attribute routed.

So basically if you use attributes then the routes defined in UseMvc or UseMvcWithDefaultRoute will be ignored. Only the attribute would be used in that case.

You could still use multiple route attributes if you wanted to achieve a similar effect than the default route with optional segments. Again from the same article in the asp docs:

public class MyDemoController : Controller
{
   [Route("")]
   [Route("Home")]
   [Route("Home/Index")]
   public IActionResult MyIndex()
   {
      return View("Index");
   }
   [Route("Home/About")]
   public IActionResult MyAbout()
   {
      return View("About");
   }
   [Route("Home/Contact")]
   public IActionResult MyContact()
   {
      return View("Contact");
   }
}
like image 89
Daniel J.G. Avatar answered Nov 10 '22 09:11

Daniel J.G.