The routing system is the first barrier that requests a message into the ASP. NET WEB API message processing pipeline, whose fundamental purpose is to use the Registered routing table (routetable) to parse the requested URI to determine the name of the target Httpcontroller and action. and a route variable that is bound to a parameter of the target action method.
The ASP. NET WEB API framework is an abstract message processing pipeline that is independent of the transport layer and does not have the ability to listen, receive, and respond to the transport protocol itself.
The two typical boarding modes, webhost and selfhost, use two distinct mechanisms for the adaptation of the ASP. NET WEB API routing system.
The core framework of the ASP. NET Web API, which includes a routing system, is a message processing pipeline that is independent of the hosting environment, which is why it can support the root cause of many different boarding patterns. The purpose of the service homestay is to provide a running environment to listen for and receive requests from the outside world, and submit the request to the abstract message processing pipeline for processing, and finally return the response generated by the pipeline to the client using the network transport. For webhost, the real routing function is done by the routing system of the ASP. The route of ASP. NET MVC is entirely done by the ASP, but the latter is not specifically designed for MVC, it was originally designed to help the Web Forms application to "detach the request address from the physical file".
A simple example to register the ASP. NET route Map
The registration of this route includes: the default value of the map URI segment (Segment), the constraint of the value, and some additional data.
1 usingSystem;2 usingsystem.web;3 usingSystem.Web.Routing;4 5 namespacewebhost6 {7 Public classglobal:httpapplication8 {9 protected voidApplication_Start (Objectsender, EventArgs e)Ten { One //set the default value for each segment of the route map URI A varDefaults =NewRouteValueDictionary - { -{"AreaCode","010"} the }; - - //to set a constraint on a value - varconstrains =NewRouteValueDictionary + { -{"AreaCode",@"0\d{2,3}"}, +{" Days",@"[1-3]"} A }; at - //set up additional data - varDatatokens =NewRouteValueDictionary - { -{"defaultcity","Beijing"}, -{"defaultdays","2"} in }; - to //registering routes in the routing table (friendly URLs separated from physical files) +RouteTable.Routes.MapPageRoute ("default","{areacode}/{days}","~/weather.aspx",false, defaults, constrains, datatokens); - } the } *}
Write a WebForm page to render the various routing data described above
1 usingSystem;2 usingSystem.Web.UI;3 4 namespacewebhost5 {6 Public Partial classWeather:page7 {8 protected voidPage_Load (Objectsender, EventArgs e)9 {Ten if(!IsPostBack) One { A if(Routedata.route! =NULL) - { - Response.Write (RouteData.Route.GetType (). FullName); theResponse.Write ("<br/>"); - } - if(Routedata.routehandler! =NULL) - { + Response.Write (RouteData.RouteHandler.GetType (). FullName); -Response.Write ("<br/>"); + } AResponse.Write ("<b>routedata values:</b></br>"); at foreach(varVariableinchroutedata.values) - { -Response.Write ($"<li>{variable. Key}={variable. Value}</li>"); - } -Response.Write ("<b>routedata datatokens:</b>"); - foreach(varVariableinchroutedata.datatokens) in { -Response.Write ($"<li>{variable. Key} = {variable. Value}</li>"); to } + } - } the } *}
Run results
ASP. NET Web API Routing