ASP.NET component design step by step (9)

xiaoxiao2021-03-06  43

First analyze the procedure for ASP.NET to process a web request. Learn ASP.NET technology, in addition to changing the documents of others (this is the most basic, knowledge is knowledge, unknown basic knowledge can't learn, you should also learn to ponder, do your internship.

When we send a web request from the client, we reach the web server, process it by the web server or transfer to the ASP.NET framework. If there is no web server (using ASP.NET default web processing), then you can join the "Cassini source code" http://blog.9cbs.net/shanhe/archive/2004/11/176422 .aspx and "Implement your own ASP.NET host system" http://blog.9cbs.net/shanhe/archive/2004/05/27/5429.aspx.

Suppose how to get the framework of ASP.NET, how to deal with?

The ASP.NET system frame has an HTTP processing logic. This logic is specified by the section of Machine.Config's name HTTPRuntime

. . .

Tell the ASP.NET Framework A application's HTTP handler default how to determine the processing logic. How to deal with the processing of the request for the HTTP predicate (VERB) and the location of the resource (PATH) combination determined.

E.g:

VERB = "*"

PATH = "*. aspx"

TYPE = "system.web.ui.pagehandlerfactory" />

Tell the ASP.NET should use the System.Web.ui.PageHandlerFactory class processing request when handling any requests for any ".aspx" file (post / get / head / put). This class is the default class of the system framework, follow the System.Web.ihttPHandler interface (but is implemented by MS). If you study the ASPX page framework, you can write an ASPX page factory handler. After signing, add a global application, and then modify it, let the ASPX page can be done in accordance with the logic of its own (you even You can not follow the page controls to implement logic, although there is no great significance).

Let's make an experiment: We use the browser to issue a request for .config files (such as web.config), but save the ASP.NET reports an error, but if we delete the machine.config

VERB = "*"

PATH = "*. config"

TYPE = "System.web.httpForbiddenHandler" />

And save. After re-requested the file, you will see that the browser can read this file and display. Re-requested the system report error. This fully illustrates the HTTP treatment plant to process according to the configuration file. According to this type, we can read the other related stages of machine.config, and fully understand how the system default processing logic handles web requests (specific for legal HTTP requests). In addition, Web.config can also increase / delete the specified HTTP handler accordingly. Refer to MSDN, we found that any HTTP handler is actually an ASP.NET class that implements the System.Web.ihttphandler interface. Interface should implement an attribute and an excuse:

Public property

IsReusable gets a value that indicates whether other requests can use the IHTTPHandler instance. Decided whether this handler can be reused (usually overcome the improving system performance) public method

ProcessRequest enables HTTP web request by implementing a custom HTTPHANDLER for the IHTTPHANDLER interface. That is, the implementation.

Suppose we write an HTTP handler, how to make him play a role? For example, we need to prohibit ASP.NET download .info file, we should do the following:

The first step is to add an instruction on the web server, allowing the ASP.NET to process this extension. Specifically:

Open IIS (assuming the web server is IIS), find the application, configuration, application mapping, add mapping, enter the current .NET version of Aspnet_isapi.dll in the executable (such as C: /Winnt/Microsoft.Net/framework/ V1.0.3705 / askNET_ISAPI.DLL), extension input .info, predicate, check if the file exists. After these steps, make sure IIS does not work with your own manner. Info file, let ASPNET_ISAPI.dll processed, the latter will hand over the request to the ASP.NET.

Step 2, add the Transfer Configuration Festival in Machine.config or Web.config. Both join sub-festival in , specify according to the format, such as:

...

...

After the above section configuration, .INFO file will be protected, any attempt to access will be informed "This type of page is not available." .

Below to implement an example of an HTTP handler, we add an extension .img, IMG file generates a picture according to the parameter, such as we request abc.img to the ASP.NET framework, then the system returns Content as image / jpg format pictures, pictures Content is ABC

The source code is as follows:

Using system;

Using system.Web;

Using system.drawing;

Using system.drawing.drawing2d;

Using system.drawing.image;

Namespace iMyweb

{

Public class img: System.Web.ihttphandler

{

Public img ()

{}

Public Bool IsReusable

{

Get {return true;}

}

Public void processRequest (httpcontext context)

{

String vStr = _GetViewString (context);

Context.response.contentType = "image / jpeg";

Image img = new bitmap (128, 128, pixelformat.format32bppargb);

Graphics g = graphics.fromImage (img);

Brush backbrush = new solid brush (color.gray); // Gray Brush Textbrush = New Solidbrush (Color.Black);

G.FillRectangle (Backbrush, 0, 0, 128, 128);

FONT FT = New Font ("Arial", 32);

g.drawstring (VSTR, FT, Textbrush, New Rectanglef (0, 0, 128, 128), New StringFormat (StringFormatflags.Nowrap);

Img.save (Context.Response.OutputStream, Imageformat.jpeg);

Context.Response .flush ();

Backbrush.dispose ();

Textbrush.dispose ();

g.dispose ();

img.dispose ();

Return;

}

// ************ //

Private string _GetViewString (HTTPCONTEXT Context)

{

String str = context.Request.Rawurl;

INT L1 = Str.lastIndexof ("/");

INT L2 = str.lastIndexof (".");

Return Str.Substring (L1 1, L2-L1-1);

}

}

}

After compiling, get the application for myhttphandler.dll.

Now, we need to tell the ASP.NET application (assuming our web application under localhost / webapp1), how to handle .img files:

The first step is to add an instruction on the web server, let the ASP.NET handle this. IIMG extension (let IIS rest, don't intervene ASP.NET, let ASP.NET processing * .img)

Second, in the section in Web.Config:

In order to enable the ASP.NET program to be able to locate the application set, copy myhttphandler.dll to the BIN directory of the web application

Next, we test: Please request a .img resource at the test web application webapp1, will get a JPG image. http://localhost/webapp1/test.img

转载请注明原文地址:https://www.9cbs.com/read-55001.html

New Post(0)