Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do custom routing in ASP.NET Core 3.1 MVC

I can't use simple routing like in .NET Core 2.2 in .NET Core 3.1.

What is the routing last change in .NET Core 3.1?

like image 900
LPLN Avatar asked Dec 07 '19 06:12

LPLN


People also ask

How is routing done in ASP.NET MVC?

Basically, Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table.

What is custom routing in MVC?

In the custom routing mode MVC sites respond to incoming requests using standard ASP.NET routing. Page URLs are determined by the routes that you register into your MVC application's routing table.

How can add route in ASP.NET MVC?

Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .

How do you implement routing in .NET core?

Routing uses a pair of middleware, registered by UseRouting and UseEndpoints: UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline.


1 Answers

In .NET 3 you should use Endpoint instead of Routing

app.UseStaticFiles();
app.UseRouting();
//other middleware

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
    endpoints.MapHub<MyChatHub>();
    endpoints.MapGrpcService<MyCalculatorService>();
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});
like image 84
Hamed Hajiloo Avatar answered Sep 29 '22 04:09

Hamed Hajiloo