When we learn to a certain extent, we will want to have a deeper understanding of the underlying things in the code, and we also want to have a framework of our own. Of course, this is also the case for bloggers. This article may be the beginning of writing a webapi framework. Friends who have studied the MVC framework will find that the routing MvcRouteHandler of the MVC framework implements IRouteHandler to implement our routing, and IRouteHandler only needs to return one object, which is IHttpHandler, and IHttphandler handles http requests. With joy, we found that we already have the core things for writing a webapi, routing and request processing. Maybe this series of articles will not explain in depth what these two things are, but focus on using them. Friends who are interested can learn about it by themselves, which will be of great help in understanding the network request processing under .net.
Without further ado, let’s start directly
We directly create a completely empty asp.net web project
Uncheck anything.
Then, we add a BaseRouteHandler, inherited from (implemented) IRouteHandler, the code is as follows
public class BaseRouteHandler:IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new BaseHttpHandler(); } }
We see that a BaseHttphandler is returned, This is what we wrote ourselves
Create a new BaseHttpHandler to implement IHttpHandler. It is worth noting that if you need this Handler to handle sessions, you only need to inherit IRequiresSessionState. This interface is just a mark 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("您正在请求BaseHttpHandler,请求方式是{0},queryStr={1}", method,request.QueryString); response.ContentType = "application/json"; response.Write(result); response.End(); } }
IHttpHander has only two things, one is IsResuable and the IsReusable attribute. MSDN explains it this way: Get a value that indicates whether other requests can use the IHttpHandler instance. That is to say, subsequent Http requests can 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 anymore
The other one is ProcessRequest, this is 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 the route
We add a global global application class to 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())); } }
At this point, the project can run normally, so we compile and run it anxiously, enter the address
Pay attention, Since our route is registered with api, adding the /api route after our project address bar can normally request to our custom HttpRouteHandler.
Therefore, we learned that there are three most basic points:
1. Implement IRouterHandler,
2. Implement IHttpHandler,
3. Register route
If you are itchy, then go and implement your own framework!
to be continued. . .
The above is the detailed content of The beginning of writing a webapi framework. For more information, please follow other related articles on the PHP Chinese website!