Mvc6 Routing (RoutePrefix and AreaPrefix)

I started looking to port my applications to MVC6 to take advantage of the new framework. One of the biggest issues I have faced is routing (attribute routing only, I ended up deleting default routes).

My apps are usually configured like this:

Area / Administration / Controllers Area / Clients / Controllers / Controllers - elements without zone

In each area, I have a base controller (such as AdminBase, CustomerBase) that will contain an [Area] tag with "AreaPrefix", and each controller in that area inherits it and specifies [RoutePrefix].

A problem arises: if I want to include Area / Controller in routes, each ActionResult must contain [Route ("[area] / [controller] / [action]")] to generate output, but if, for example, I have a controller "ChangeAddress ", I would prefix it as" change-address "in the url, but I don't see any way, without explicitly placing it on all routes in the controller, how to do this?

+3


source to share


1 answer


In MVC6, you still have a scope and a prefix, it is just assigned only to the Route tag for the controller level and puts the action route in an http verb. so that.

[RouteArea("AreaPrefix")]
[RoutePrefix("RoutePrefix")]
public class HomeController : Controller
{
    ...
    [HttpGet]
    [Route("LoginAction")]
    public ActionResult Login() ....

      

becomes

[Route("AreaPrefix/ControllerPrefix")]
public class HomeController : Controller
{
    ...
    [HttpGet("LoginAction")]
    public IActionResult Login() {

      



So, this is more ambiguous, but I was personally the culprit in the self-criticism of the routing process, and where this uses standard routing objects in [] notation (think of it like it was before {}). But it still gives you the ability to control the level of a single and subsequent level, so now it gives you a simpler inheritance model:

[Route("AreaPrefix/ControllerPrefix")
public class HomeOps : Controller // Controller Suffix is optional
{

}

[Route("api")]
public class HomeApiOps : HomeOps
{
     // put the api AJAX methods here
     [HttpPost("lookup")] // will route to // AreaPrefix/ControllerPrefix/api/lookup
     public IActionResult Lookup() {
}

public class HomePageOps : HomeOps
{
     // put similar routes for View returns, file content responses here
     [HttpPost("lookup")] // Will route to AreaPrefix/ControllerPrefix/lookup
     public IActionResult Lookup() {

}

      

This can lead to a more organized way of expressing Units of Work and Line of Business, where you can easily group and at the same time separate api calls and pages without a massive controller or infinite #region chunks.

With this inheritance, also remember that you could create a root controller class for each scope and define specific routes for the layered layer, and then inherit the second layer for the "ControllerPrefix" service route, and only need to point to each action the name of the action route it uses a set of inheritance to glue routes together, flipping the previous example in AreaPrefix / ControllerPrefix / Route

I'm alone with you with MVC6, client side developers in my business love the platform and at the end of Data it makes their information clean and simple, but the sparse documentation can get frustrating sometimes ... Hope this helped

+8


source







All Articles