asp.net 2.0 want to say love you not easy

Source: Internet
Author: User
asp.net

In this paper, some problems encountered in developing ASP.net wildcard mapping application in asp.net 2.0 are analyzed to illustrate the deficiencies of the page compilation model in asp.net 2.0. If there are any irregularities in the article, you are welcome to point out.

The ASP.net wildcard mapping application here refers to forwarding all requests in IIS to asp.net 2.0 run-time processing (for IIS 5.0, which is to establish a. * to aspnet_isapi.dll Mapping), By implementing the System.Web.IhttpHandlerFactory interface in the program to handle all requests, the System.Web.IhttpHandlerFactory class is equivalent to a front-end controller. The typical application is. Text and based on the. Text Development Blog Park blogging software.

In ASP.net 1.1, the implementation of wildcard mapping applications may be more clear, mainly two points:

1. Implement System.Web.IhttpHandlerFactory interface, in GetHandler (HttpContext context, string RequestType, string URL, string path) According to the requested URL, based on some rules, locate the actual paging file that is actually accessed, and then call Pageparser.getcompiledpageinstance to compile the page and generate the corresponding instance processing request. The advantage of this is that you can use any URL address, do not care about the existence of the corresponding paging file, and can easily control access to resources on the Web server.

2. Add in the web.config:



The new page-compilation model in asp.net 2.0 brings unexpected problems to the implementation of wildcard mapping applications, and I take blog Park blog Software as an example to discuss these issues with you.

In blog software, the implementation of IHttpHandlerFactory interface is Dottext.Common.UrlManager. Urlrewritehandlerfactory, do not change the Urlrewritehandlerfactory code implemented in ASP.net 1.1, compiled and run directly in ASP.net 2.0, when the program runs in the IIS root directory, The "Object reference not set to a instance of an object" exception appears when the Pageparser.getcompiledpageinstance is executed (this problem does not occur in the virtual directory). This problem is a small bug in asp.net 2.0, A bug in the pageparser.getcompiledpageinstance I wrote before and a solution to this problem are analyzed, which can be done by calling Context.re before Pageparser.getcompiledpageinstance Writepath ("~/default.aspx") resolved.

Then the test to access the personal blog home page, such as: Http://www.cnblogs.com/dudu, Error accessing:

"There is no builds provider registered for the extension". Can register one in the <compilation> <buildProviders> section in Machine.config or Web.config. Make sure are has a Buildproviderappliestoattribute attribute which includes the value ' Web ' or ' all '.

In ASP.net 2.0, when we first visit a page, a lot of two processes are: 1, page compilation 2, create the compiled page code instance. Page compilation compiles a page by finding a matching build provider based on the extension in the URL address you are accessing. The problem here is that the ASP.net 2.0 runtime did not find the corresponding build Provider, and for Http://www.cnblogs.com/dudu this address, because the wildcard mapping was used, the ASP.net 2.0 run-time processing, The resulting extension is empty (if no wildcard mapping is used, IIS automatically changes the address to: http://www.cnblogs.com/dudu/default.aspx). The ASP.net 2.0 is not designed to address this situation, and it is not possible to solve the problem by configuring it in the web.config. If you can provide the following configuration, this problem can be easily resolved:



My solution to this problem is to process the URL before pageparser.getcompiledpageinstance, and add the default file to the URL, such as: Default.aspx. If you want to use a different extension, such as:. HTML, you need to add the following in the web.config:



There is also a bug in the error message "Make sure are has a BuildProvider Appliestoattribute attribute which includes the value ' Web ' or ' all '. "Prompted to set the Appliestoattribute property, in fact Web.config does not support such a property, may be the official version of the ASP.NET 2.0 supported this property, and then removed, the error message is not modified.

Solves the above two problems, originally thought the wildcard mapping application can run normally in the ASP.net 2.0, I test the blog Garden's program on this machine, the page can the normal visit. But this morning, after the server into the Web site upgrade to ASP.net 2.0, found that the ASP.net runtime in the frequent compilation of the page, CPU consumption has been 100%, compiled one hours is still compiling, and the compilation seems to be related to the number of visits, less access to the Site page can also open, Blog Park main station due to the large number of visits, almost inaccessible. What's the problem? So I looked for clues from the source code of Pageparser.getcompiledpageinstance, found suspicious in Buildmanager.getcachekeyfromvirtualpath, BuildManager based on the requested virtual path to create a slow And then find or create the cached object after the page is compiled based on this key. When a request is made to a page, BuildManager checks the cache, checks it from memory, if it is not in memory, finds it from the cached folder (temporary asp.net Files), and if found, creates an instance of the type directly without dynamic compilation. If not found, the page compiles work, and the basis of the lookup is based on the virtual path created by the cache key.

This does not cause problems with the usual way of page access. However, there are problems with wildcard mapping. Because of wildcard mapping, it is common for different URL addresses to access the same paging file. For example, Blog Park in each article address is different, but access is the same page code, if the current ASP.net 2.0 in the page to compile the model, each article first access to compile, if the blog park in hundreds of thousands of articles are visited, it is necessary to carry out hundreds of thousands of compilation, No wonder the server has been busy compiling the blog site since it was upgraded to ASP.net 2.0 today.

After the test, sure enough, of course, visit Address: http://www.cnblogs.com/dudu/archive/2006/03/07/345107.html will be in temporary asp.net Files in the folder compiled to generate class asp.dudu_archive_2006_03_07_345107_html, and access to other articles, but also based on the article address to generate another class. So the compilation efficiency is too low! Why to create a slow key based on the virtual path, the designer did not consider the problem of wildcard mapping at all, really bad design! This problem can be easily avoided if you create a cache key based on the actual access page file name, as in ASP.net 1.1. asp.net 2.0 The new page-compilation model here seems to be a flop. Worse, there is no opportunity for developers to make up the bug, and System.Web.Compilation.BuildManager does not provide a way or a property for developers to set their own caching keys.

(Note: The way to create a cache key is BuildManager.) Getcachekeyfromvirtualpath (virtualpath virtualpath, out bool KEYFROMVPP)). To make things worse, many of the classes in the System.Web.Compilation are internal, and many of the classes are grey (reflector display internal static or private with a gray color, which is good for a person to look discouraged), It is almost impossible to call your own method for page compilation (with the method of reflection can not know whether to call, have not tried, even if you can call, also consider the loss of performance).

Do you want to write your own System.Web.Compilation of those classes to handle page compilation? I would rather choose ASP.net 1.1, and then ASP.net 2.0 sp1,sp1 can not solve, such as SP2 ... Hope not to wait until ASP.net 3.0.

Maybe you thought of it. In GetHandler (HttpContext context, string RequestType, string URL, string path) To compile and create an instance of the page by calling System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath. I have tried this method, the answer is no, not as good as pageparser.getcompiledpageinstance, at least the latter can make the program run. When using Buildmanager.createinstancefromvirtualpath, the "The resource cannot be found" error occurs when an address is accessed without an extension. The reason is in getvpathbuildresultinternal (virtualpath virtualpath, bool nobuild, BOOL Allowcrossapp, BOOL Allowbuildinprecompile), util.checkvirtualfileexists (virtualpath) is called to examine the virtual path, convert the virtual path to a physical path when checking, and check whether the currently requested paging file exists. For wildcard mapping applications, many addresses do not actually exist, so the "the resource cannot be found" error occurs. In pageparser.getcompiledpageinstance, this problem is avoided by calling Hostingenvironment.addvirtualpathtofilemapping. And this method is
Internal protection, also cannot be invoked in code.

I think the crux of the problem is that the ASP.net 2.0 designers did not consider the wildcard mapping in their design. Whether it is ignored or otherwise considered, is unknown. But ASP.net 1.1 can handle the problem correctly, and ASP.net 2.0 can't handle it, it's not right here. Web application users mapping with wildcard characters can only look at ASP.net 2.0 hearts sigh. Recently spent a lot of effort to move the blog program to ASP.net 2.0, but the result is unable to migrate to ASP.net 2.0, disappointing! Can only hope that Microsoft introduced the corresponding patch.

Fortunately, there are not many Web applications that use wildcard mappings, and the problem is not very significant.



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.