What are routes in ASP.NET MVC?

5

In ASP.NET MVC I always get the term route when #, however, I started to study APS.NET MVC a short time ago and this term route / strong> this is causing me confusion.

Question

I would like to know what are routes and what is their purpose and also what is the importance of them in relation to my ASP.NET MVC application?

If possible I would like you to give examples of preference in the C # language. But feel free to sample in other languages supported by DotNet.

    
asked by anonymous 29.10.2016 / 02:41

2 answers

6

I think you want to know about routing as a whole. It is the way to direct incoming HTTP requests to the appropriate controller methods. So as the information comes in this request a different method will be called.

The route is the path it will take to execute something, depending on the context, somehow confused with the URL . In a way we can say that it is the method within the class. The path is decided according to the HTTP verb, the various parts of the URL or some other relevant information to make a decision.

The ASP.Net MVC routing system analyzes the incoming request, looks for a pattern, and uses a criterion to decide what to do. Details about this can be configured programmatically in addition to the standard that it adopts by convention according to the strings .

In the older versions it used the same classic ASP.Net routing system. In the .Net Core there is a system of its own.

Example

An example route created in class RouteConfig :

routes.MapRoute("Default", //nome da rota
                "{controller}/{action}/{id}", // padrão do URL
                new { controller = "Home", action = "Index", id = "" }  // parâmetros

Then what comes first at the beginning of the parameters in the URL (under normal conditions right after the domain and perhaps the port) is what will determine which controller class will be called.

What comes next as if it were a subdirectory is the action, that is, the method of this class that should be executed. But it may be that other parts are required to determine the correct method since the methods may have the same name but different signatures , then you need to understand the type of data that will be passed if you have methods with the same name.

Finally, in this example, find what will be passed to the method as argument. Your type could be considered in the signature.

The object with default values is also set if there is nothing useful to establish the route.

The class that will process this would look something like this:

public class HomeController : Controller {
    public ActionResult Index(string id) {
        return View();
    }
}

You could have a note indicating the specific verb that is accepted.

I could call it like this:

dominio.com/Home/Index/101

Attributes

You can also define routes with attributes . Example:

[Route(“{produtoId:int}”)]
public ActionResult Edita(int produtoId) {
    return View();
}

Alternative to routing

All routes form a mapping between what comes from outside and the code. Without the routes would have to have a file for each action and the interpretation of the arguments contained in the URL would have to be made in each of these files. This engine takes care of boring and risky work, making it much easier for the programmer to work.

Conclusion

It has several details, but by the question the basic idea is this. Specific questions can be interesting. Check out what has already been asked here on the site.

29.10.2016 / 03:27
5

The routes would be the way to access the Controllers Actions (or basically, the URL).

Example:

Let's say you have a Controller called the Customer Area and inside this controller have an Action call ListBuy.

To access this Action, you would have to type the URL in the following format:

www.example.com/AreaClient/ListBuy

This is basically the Route.

Its importance is in the access to the Actions and also in the business [Friendly URL and SEO (set of good practices to increase the reach by site of searches, like Google, Bing, etc.)]. MVC has some tools that help maintain these routes. An example is RouteConfig , but also make some other methods available, such as attributes .

An example is RoutePrefix , which is used above the controller name to establish a prefix (name for controller access), if you want something more enjoyable. Using the above example, we could use it as follows:

[RoutePrefix("cliente")]
public class AreaCliente : Controller
{
   // Actions ...
}

In this way, we can have the possibility to access as follows:

www.example.com/ customer / ShoppingWrap

And to fit even better, we have another attribute called Route , this is used above the action:

[RoutePrefix("cliente")]
public class AreaCliente : Controller
{
    [Route("compras")]
    public ActionResult ListaCompras()
    {
        // código
    }
}

And in this case, we would access in the following format

www.example.com/ customer / purchases

Other attribute characteristics are also modulate the Action parameters.

    
29.10.2016 / 03:05