This is the second day of my participation in the Novembermore Challenge

1. The introduction

When we create a WebApi project using the VS tool, we create the controller, and then the browser or client accesses the url. How does WebApi find the specified resource?

In Web API, certain format rules are preset into the system in advance. When the browser or client requests data resources, it needs to obtain them in the specified format, and the specified format is the Route of webAPI.

2. How are routes registered in WebApi?

An ASP.NET Web application has a global routing table, which is represented by the Routes static property of type RouteCollection in a RouteTable class.

Some of you might wonder why we set Routes to static?

In fact, I think it is static because resident memory cannot be changed and can not be released during the entire application life cycle. Routes is like a small database, and this “database” in a sense stores the information of route registration.

public class RouteTable
{
    private static RouteCollection _instance = new RouteCollection();

    public static RouteCollection Routes => _instance;
}
Copy the code
3. How to explain RouteTable?

1. First, we need to open a WebAPI application to analyze. We know that in Aspnet, the web application is launched by the Global class, and the route registration exists in the Global class Application_Start method.

public class WebApiApplication : HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); }}Copy the code

2. In the code above I removed some of the other code, we in the web application starts, first call GlobalConfiguration. Configure (WebApiConfig. Register) method, This method takes an Action</HttpConfiguration/> as an argument. Our first response to Action should be a callback, passing the HttpConfiguration argument externally when executing internally, giving us room to expand.

3. We found the code of WebApiConfig.Register

public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }}Copy the code
4. Understand the essence of adding routes to the framework

We see the Register method, the framework to join routing calls is config. The Routes. MapHttpRoute method

There are two separate steps to interpret it:

1. Config. Routes is what?

Config.Routes:

  1. Config. Routes definition, which takes the internal private variable _routes
public HttpRouteCollection Routes { get { return _routes; }}Copy the code
  1. _routes Corresponds to the HttpRouteCollection type passed in the HttpConfiguration construction
_routes public HttpConfiguration(HttpRouteCollection routes) { _routes = routes; }Copy the code
  1. Found in GlobalConfiguration HttpConfiguration initialization code, find the corresponding argument is a HostedHttpRouteCollection HttpRouteCollection type
  2. HostedHttpRouteCollection also receive aRouteTable.Routesparameter
// Private static Lazy<HttpConfiguration> CreateConfiguration() {return new Lazy<HttpConfiguration>(() => { HttpConfiguration config = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes)); return config; }); }Copy the code

Conclusion 1

1.config.Routes == new HostedHttpRouteCollection(RouteTable.Routes); 2. The passed parameter routetable. Routes is the global routing table in the Web application.Copy the code
2. What exactly does MapHttpRoute do?
CreateRoute is called to create an IHttpRoute instance. // Add routes to Add public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler) { HttpRouteValueDictionary defaults2 = new HttpRouteValueDictionary(defaults); HttpRouteValueDictionary constraints2 = new HttpRouteValueDictionary(constraints); IHttpRoute httpRoute = routes.CreateRoute(routeTemplate, defaults2, constraints2, null, handler); routes.Add(name, httpRoute); return httpRoute; }Copy the code

Conclusion 2

1. IHttpRoute instance is completed by the CreateRoute HostedHttpRouteCollection; Routes. Add calls routetable. routes to Add the name and route to its binding.Copy the code
5. Simple diagrams