English 中文(简体)
“UseRouting”方法如何在ASP中发挥作用。 NET Core?
原标题:How does the "UseRouting" method work in ASP.NET Core?

I ve read that UseRouting matches a request to an endpoint, and UseEndpoints executes the matched endpoint. However, we have middleware in this order:

`...Another Middleware

UseRouting(); 

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");


app.Map("/something", (context) => context.Response.WriteAsync("Hello World!"));

...Another Middleware`

因此,如果在马普勒罗特省援引使用,则使用方法如何与要求(如https:// localhost:5000/Home/Index)相匹配。 换言之,使用方法如何知道这一终点(Home/Index)来进行比较?”

我没有尝试过任何事情,因为这是一个理论上的问题。

问题回答

The endpoint is always null before UseRouting is called.If a match is found, the endpoint is non-null between UseRouting and UseEndpoints.The UseRouting middleware uses the SetEndpoint method to attach the endpoint to the current context.The UseEndpoints middleware is designed to be used in tandem with the UseRouting middleware.

// Location 1: before routing runs, endpoint is always null here.
app.Use(async (context, next) =>
{
    Console.WriteLine($"1. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
    await next(context);
});

app.UseRouting();

// Location 2: after routing runs, endpoint will be non-null if routing found a match.
app.Use(async (context, next) =>
{
    Console.WriteLine($"2. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
    await next(context);
});

// Location 3: runs when this endpoint matches
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

结果:

“enterography

您可读到ASP.NET。 核心终点定义以了解更多情况。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签