Mehdi Mowlavi
Mehdi Mowlavi

Reputation: 462

What if the difference between Map method of IApplicationBuilder and Map method of IEndpointRouteBuilder in ASP.Net Core

Since WebApplication implement both of IApplicationBuilder and IEndpointRouteBuilder there is a Map method for application with multiple overloads specially Map(string pattern,Action<IApplicationBuilder>) and Map(string pattern, RequestDelegate requestDelegate). What is the difference between them?

Upvotes: 5

Views: 1481

Answers (2)

Lanayx
Lanayx

Reputation: 2876

Main differences:

  • IApplicationBuilder.Map branches based on start of the url, while IEndpointRouteBuilder.Map branches based on the full url
  • IEndpointRouteBuilder.Map handler is always terminal while IApplicationBuilder.Map allows further pipeline execution by calling next.Invoke()

Upvotes: 0

Mehdi Mowlavi
Mehdi Mowlavi

Reputation: 462

Although thy have same name, they have almost different purposes and overload. The map method of application builder (Map(string pattern,Action<IApplicationBuilder>)) first create a new app using IApplicationBuilder.New() method and then apply to this new Application, the configuration defined in Map method using Action<IApplicationBuilder> configuration. At the end it use the Build method of New IApplication and create a request delegate. Then invoke this delegate using path matched as base path and remaining path as path in a middleware named MapMiddleware (it has nothing except setting and resetting basepath and path from passed one to matched one and reverse! and invoking the built request delegate). See following link:

IApplicationBuilder Map

But The Map method of IEndpointRouteBuilder (Map(string/RoutePattern pattern, RequestDelegate requestDelegate)) first create a new RouteEndpointBuilder() with order=0 and displayname as pattern raw text from pattern and delegate. Then add attributes of RequestDelegate if any (in class based request delegate like controllers) to builder. Then it check if there is any EndpointDataSource exist for IEndpointRouteBuilder and if not create one (for example using new ModelEndpointDataSource()) and add it to IEndpointRouteBuilder.DataSources. At the end it build an IEndpointConventionBuilder from the RouteEndPointBuilder for example using new DefaultEndpointConventionBuilder(endpointBuilder) and it to data source (Actually it should build the route endpoint and it to data source but we can implement the getter for endpoints in a way that read from convention builder) and Finally return that convention builder to do more work on it. see following:

IEndpointRouteBuilder Map Method

Upvotes: 2

Related Questions