What are ASP.NET MVC routes?

Asked

Viewed 565 times

6

In ASP.NET MVC always appears to me the term route when I read something related to this technology, however, I started studying APS.NET MVC recently and this term route is causing me trouble.

Question

I’d like to know what they are routes and what is their purpose and also what is their importance in relation to my application made in ASP.NET MVC?

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

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 proper methods of controller. Then as the information comes in this request a different method will be called.

The route is the path he’ll take to execute something, depending on the context, somehow gets 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, searches for a pattern, and uses a criterion to decide what to do. Details about this can be set programmatically, in addition to the standard it adopts by convention according to the strings.

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

#Example

An example route created in the class RouteConfig:

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

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

What comes next as a subdirectory is the action, that is, the method of that class that must be executed. But it may be that other parts are needed 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, you find what will be passed to the method as argument. Its type could be considered in the signature.

You also set the object to default values if nothing useful to set the route.

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

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

Could have a note indicating the specific verb which is accepted.

I could call it that:

dominio.com/Home/Index/101

#Attributes

It is also possible to define routes with attributes. Example:

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

I put in the Github for future reference.

#Alternative to routing

All routes form a map between what comes externally and the code. Without the routes it would have to have a file for each action and the interpretation of the arguments contained in the URL would have to be done in each of these files. This mechanism takes care of the boring and risky work making the work of the programmer much easier.

#Completion

There are several details, but the basic idea is this. Specific questions can be interesting. See before what has been asked here on the site.

5

Routes would be the way to access Controller Actions (or basically, the URL).

Example:

Let’s say you have a Controller call Areacliente and within this controller have a Action call Listshopping.

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

www.siteexemplo.com/Areacliente/Listshopping

This is basically the Route.

Its importance is in accessing Actions and also in business [User Friendly URL and SEO (set of best practices to increase reach by search sites, such as Google, Bing, etc)]. MVC has some tools that help maintain these routes. An example is by Routeconfig, but also make available some other methods, such as attributes.

An example is the Routeprefix, which is used above the controller name to set a prefix (controller access name) if you want something more pleasant. Using the example above, we could use in the following form:

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

In this way, we can have the possibility to access in the following way:

www.siteexemplo.com/client/Listshopping

And to adjust 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 the following format

www.siteexemplo.com/client/shopping

Other attributes characteristics are also modular Action parameters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.