Callback, it seems that you can build a webapi framework-IRouteHandler, iroutehandler
When we learn a certain degree, we will want to gain a deeper understanding of the underlying code and have a framework of our own. Of course, this is also true for bloggers. This article may be the beginning of a webapi framework. If you study the MVC framework, you will find that the MvcRouteHandler of the mvc framework implements IRouteHandler to implement our routing, while the IRouteHandler only needs to return a pair, that is, IHttpHandler, IHttphandler processes http requests. We are delighted to find that we already have the core of compiling a webapi, routing, and request processing. Maybe this series of articles will not explain exactly what these two things are, but will focus on using them. If you are interested, you can understand them yourself. net.
Let's get started.
Create a completely empty asp.net web project.
Do not select anything.
Then, we add a BaseRouteHandler that inherits the self (Implementation) IRouteHandler. The Code is as follows:
public class BaseRouteHandler:IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new BaseHttpHandler(); } }
We can see that a BaseHttphandler is returned, which is compiled by ourselves.
Create a BaseHttpHandler to implement IHttpHandler. It is worth noting that if you need this Handler to process the session, you only need to inherit the IRequiresSessionState. This interface is only a tag and does not require any implementation.
Public class BaseHttpHandler: IHttpHandler {public bool IsReusable {get {return false;} public void ProcessRequest (HttpContext context) {var request = context. request; var response = context. response; var method = request. httpMethod. toLower (); var result = string. empty; result = string. format ("you are requesting BaseHttpHandler. The request method is {0}, queryStr = {1}", method, request. queryString); response. contentType = "application/json"; response. write (result); response. end ();}}
IHttpHander has only two things. One is the IsResuable and IsReusable attributes. This is explained in MSDN: Get a value that indicates whether other requests can use the IHttpHandler instance. That is to say, can subsequent Http requests continue to use instances of classes that implement this interface? Here we set it to false, because we do not need to inherit this Handler.
The other is ProcessRequest, which is used to process specific requests. HttpContext contains various parameters of our http request. We only need to process the context data.
Before accessing, We need to register a route
We add a global application class for the program, delete all methods except the Application_Start method, and then write the following code
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new Route("api", new BaseRouteHandler())); } }
By now, the project can run normally, so we are eager to compile and run it. Enter the address
Note: Because the route is registered with the api, you can add the/api route after the address bar of your project to request the custom HttpRouteHandler.
As a result, we learned that there are three basic points:
1. Implement IRouterHandler,
2. Implement IHttpHandler,
3. Register a route
If you are also itchy, implement your own framework!
To be continued...