Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute routing in MVC 5 and optional defaults

Traditional routing defaults meant we were able to access these URLs and always end up on the same action:

/
/Home
/Home/Index

But today we would be writing something in these lines:

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    public ActionResult Index() {}

    public ActionResult ...
}

But this routing definition is by no means the same.

/           (fails)
/Home       (works)
/Home/Index (works)

So if we then change upper code to

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    [Route("~/")]
    public ActionResult Index() {}

    public ActionResult ...
}

But then we turn the processing upside down:

/           (works)
/Home       (fails)
/Home/Index (fails)

We could make declarative code more verbose and make it work as the old-fashioned routing mechanism by:

[RoutePrefix("Home")]
[Route("{action=Index}")]
public class HomeController
{
    [Route("~/")]
    [Route("~/Home")]
    [Route("~/Home/Index")]
    public ActionResult Index() {}

    public ActionResult ...
}

This works with all three different routes.

Question

This issue is of course bound to the very application default action that defaults controller and action. It's just that I wonder whether this is the only way of doing it? Is there any less verbose code way of getting it to work as expected?

like image 788
Robert Koritnik Avatar asked Dec 11 '13 14:12

Robert Koritnik


People also ask

Can you enable attribute routing in MVC 5?

MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application. The earlier style of routing, called convention-based routing, is still fully supported.

How is attribute routing different from default routing?

As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.

What is routing in MVC and what is a default route?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.

Which option is correct for attribute based routing in MVC?

Enabling Attribute Routing in ASP.NET MVC Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes. MapMvcAttributeRoutes() method with in RegisterRoutes() method of RouteConfig. cs file. You can also combine attribute routing with convention-based routing.


1 Answers

Yeah, right..what you have is the way to do here...

I modified the code a bit here:

[RoutePrefix("Home")]
[Route("{action}")]
public class HomeController
{
   [Route("~/")]    // GET /
   [Route]          // GET /Home
   [Route("Index")] // GET /Home/Index
   public ActionResult Index() {}

   public ActionResult ...
}

Some details:
1. Your first case is not exactly the same as conventional routing as in this case you have a literal segment Home which is not similar to the conventional routing optional of {controller}/{action}/{id} and controller = Home, action=Index,id=optional.
2. Your second case is expected as by design if a Route attribute is used on action the attributes on Controller do not take effect.

like image 131
Kiran Avatar answered Sep 22 '22 15:09

Kiran