Click here to Skip to main content
15,886,025 members
Articles / All Topics

MVC 6 Attribute Routing New Features and Differences with MVC 5

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
1 Oct 2015CPOL2 min read 14.6K   3  
MVC 6 Attribute Routing New Features and Differences with MVC 5

Introduction

One of the most important changes in MVC 6 was mixing the WebAPI and MVC, now they are not two separate things, MVC 6 now contains both of them, that means these two technologies are going to share the same code base for a lot of things, and routing is one of them. In this post, I'm going to discuss the changes that are made to the routing system in MVC 6, and look at some of its differences.

MVC 5 Routing: HttpGet Routes Mix Itself With HttpPost

One of the great features that came with MVC 5 was attribute routing, but it was not totally without faults, for example, the route values were static, or even worse the route values that were used on HttpGet version of an action, also were affecting the HttpPost version of the action, for example consider the action below:

C#
[Route("NewsLetter/SelectEmail/{page?}")]
[HttpGet]
public ActionResult SelectEmail(int? page, string priCat, string secCat)
{
 //Method body
}

Here we have "SelectEmail" method that responds to "HttpGet", now let's assume we had a method with the same name, but without any routing attribute like this:

C#
[HttpPost]
public ActionResult SelectEmail(int id)
{
//Method body
}

In MVC 5, what is happening with a situation like this is that, now our HttpPost method has the same route as our HttpGet method because we set the route on HttpGet, and now we have to set the route for HttpPost specifically as follows:

C#
[Route("NewsLetter/SelectEmail/{id}")]
[HttpPost]
public ActionResult SelectEmail(int id)
{
//Method body
}

Mvc 6 Routing: RESTful Style Routes With MVC 6 Attribute Routing

Now with MVC 6, we no longer have the problem that I mentioned above, not only that, but now we can even declare RESTful like routes like [HttpGet("Our Route")] and [HttpPost("Our Route")].

C#
[HttpGet("NewsLetter/SelectEmail/{page?}")]
public ActionResult SelectEmail(int? page, string priCat, string secCat)
{
 //Method body
}

This causes the code to be cleaner and less cluttered and we don't have the problems that we had when we were using MVC 5.

Mvc 6 Routing: Attribute Routing Dynamic Controller and Action Name

In the past, we had to specify the controller and action name specifically, and if down the road we decide to change our controller or action name, we had to change the routes too. But now with new features added in MVC 6, we can declare that we want to use the same name for some section of our route as our controller or action, like this:

C#
[Route("[controller]")]
public class MyController : Controller
{

  [Route("[controller]/[action]/{id}")]
  [HttpPost]
  [ValidateAntiForgeryToken]
   public ActionResult SelectEmail(int id)
  {

  }
}

MVC 6 Routing: Define Data Type and Default Value For Route Parameter

With MVC 6 Routing, now we can constrain the data type of our route parameters, or provide default value for it:

C#
[HttpGet("details/{id:int=99}")]
 public IActionResult Details(int id)
 {
     return View();
 }

I'm sure I've only scratched the surface of the great changes that come with MVC 6 routing, but these were the most noticeable in my opinion. If you think I've missed something, feel free to correct me by leaving a comment, have a good day!

 

This article was originally posted at http://www.hamidmosalla.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
Programming is my passion, because I find it so intellectually rewarding. I currently work as a back-end web developer, using Microsoft technology stack, I also blog about my experiences and contribute to open source projects on my free time.

Comments and Discussions

 
-- There are no messages in this forum --