Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6 Attribute Routing using the new "[controller]/[action]" Tokens and Areas

OK, I know the easiest way to use Attribute Routing in MVC 6 is:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}

And here is the controller code using the new Tokens (without Areas):

[Route("[controller]/[action]")]
public class HomeController : Controller
{
}

And here is the controller code using the new Tokens (with Areas):

[Area("MyArea")]
[Route("[controller]/[action]")]
public class HomeController : Controller
{
}

Questions:

  1. Is this how MS wants you to code your controllers using Areas and Tokens?
  2. Or is there a cleaner way?
  3. Could they have somehow created an [area] Token?

Lastly, I know I can play this game, but isn't the 1st convention-based approach - app.UseMvc() - the simplest?

public void Configure(IApplicationBuilder app)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "areaRoute",
            template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
        );
    });
}
like image 828
Moe Howard Avatar asked Jan 11 '16 03:01

Moe Howard


People also ask

What is attribute based routing in MVC?

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.

What are the action attributes in MVC?

Action selectors are attributes that can be applied to action methods and are used to influence which action method gets invoked in response to a request. It helps the routing engine to select the correct action method to handle a particular request.


1 Answers

Microsoft gives you two options, each with own pros and cons. You should decide which one is better based on your context/needs.

Convention based routing

Pros:

  • It is simpler, instead of defining everything on an per-action level you just decide once and for all how your urls will looks like .
  • Perfect when your urls exactly match controller/action names.
  • If you want to change and url you need change the name of class/method.
  • Perfect for projects with clean and predictable url structure.
  • Perfect for quickly prototyping a new project.
  • Slightly easier for developers -> by knowing an url you know in which controller/action lies the functionality

Cons:

  • You loose little bit of control

Attribute-based routing

Pros:

  • Gives you total control over how the url looks like, for example for SEO purposes.
  • If you want to change an url you do not need to change the name of class/method.
  • Perfect when your urls do not match controller and action names or you want to hand-craft them (i.e customer wants that).
  • Perfect for maintaining backward compatibility, when you have an legacy project and want to have compatible url structure.

Cons:

  • Requires little bit more work, as you need to define the routes in your code. Please note that adding an attribute to a class/method is a matter of seconds.

How to decide which one to use:

  • If you have/expect to have very few routes.MapRoute() calls -> use convention routing as its simpler

  • If you have/expect to have lots of routes.MapRoute() calls -> use attribute routing

like image 79
Mariusz Jamro Avatar answered Jan 04 '23 12:01

Mariusz Jamro