Hey, it's like I can make a webapi frame.-iroutehandler Use

Source: Internet
Author: User
Tags httpcontext

When we learn to a certain extent, we will want to learn more about the bottom of the code, but also want to have a frame of their own, of course, the blogger is exactly the same. This article may be the beginning of writing a WEBAPI framework. Friends who have studied the MVC framework will find that the MVC framework's routing Mvcroutehandler is implemented by Iroutehandler, and Iroutehandler only needs to return a pair of images, that is IHttpHandler, And IHttpHandler is dealing with HTTP requests. Rejoice, we find that we have had to write a webapi the most core things, routing and request processing. Perhaps this series of articles will not be particularly profound to explain what these two things specifically, but focus on the use of them, interested friends themselves understand oh, to understand the network request processing under. NET is of great help.

No more talk, just start.

We directly build a completely empty ASP. NET Web Project

  

Do not tick anything.

Then, we add a Baseroutehandler, inherit from (implement) Iroutehandler, the code is as follows

  

public class Baseroutehandler:iroutehandler    {public        IHttpHandler Gethttphandler (requestcontext RequestContext)        {            return new Basehttphandler ();        }    }

We see that we've returned a basehttphandler, which we wrote ourselves.

Create a new Basehttphandler, implement IHttpHandler, it is worth noting that if you need this handler to handle the session, you only need to inherit irequiressessionstate, this interface is just a token, 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,isreusable property, which is explained on MSDN: Gets a value that indicates whether other requests can use the IHttpHandler instance. This means that the subsequent HTTP request is not allowed to continue using an instance of the class that implements the interface, which we set to false, because we don't have to inherit this handler anymore.

Another is ProcessRequest, this is to handle the specific request, HttpContext contains the various parameters of our HTTP request, we only need to deal with the context of the data.

Before accessing, we need to register the routing

We add a global global application class for the program, remove 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 ()));        }    }

Writing to this, the project has been able to run properly, so we worry about the compilation run, enter the address

Note that, because we are routing registered API, so after our project address bar to add/api route can normally request to our custom httproutehandler inside.

So, we learned that the most basic of the three points:

I. Realization of Irouterhandler,

Two. Achieve IHttpHandler,

Three. Registering a route

If you also itch, then, to achieve their own framework it!

Cond...

Hey, it's like I can make a webapi frame.-iroutehandler Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.