Leading technology: Subclass and rewrite asp.net pages

Source: Internet
Author: User
Tags config require

A customer recently and found me saying "we need to make changes to some asp.net pages." Can you help me? "Like any other consultant, I immediately replied," Of course, tell me the details. "But in addition to the URLs of these pages, the customer actually offers virtually nothing." In any case, the customer is going to modify some asp.net pages without the source code. At first I thought it was just a question, but as I spoke to the client, I felt it was an interesting challenge.

If you do not create a derived class from the source code of your button, nor do you asp.net the source code of the page to modify its behavior, in Windows programming, you typically associate the underlying message with a subclass of the window. In asp.net, you can try to associate page events to override page behavior and output.

After a bit of careful thinking, I've summed up some real-world situations where you might want to modify the run-time behavior of a asp.net page without altering the source code. I've also found a lot of ways to accomplish this task. In this article (in two parts), I'll describe these methods and how to change the user interface and behavior of the ASP.net page only through read-only access or even without access to the source code.

Possible situation

Suppose a group of users is running a personalized version of a WEB application, and the application requires some remote debugging or analysis. You cannot interrupt the service, but you still need to know what happened. It may not be enough to have performance counters and built-in tracking capabilities. The deployed ASP.net page may not contain or enable trace directives at all. In this case, you need to do a remote intervention to inject the trace code, which is the most basic requirement.

There is also a situation where you need to change a variety of pages temporarily. For a single page, you simply create a copy of the old page and replace it. However, it may be more difficult to update multiple pages, and maintenance can be complex.

The third scenario is that the enterprise policy prohibits writing to the source code. In this case, the source code is readable but not modifiable. However, you can add extensions to the site.

The last case, and the most complex and I've really encountered, is that the source code for the WEB application that the company runs is no longer available for some reason.

As you can see, there are many situations where you need to modify the run-time behavior of a page without accessing the source code. So what should we do next?

A viable approach

There are many ways to modify a running ASP.net page without touching the source code. Figure 1 lists several methods. Not all methods work in any case, and some methods can be used together. Some methods may require writing a new HTTP module or HTTP handler, while others may require changes to the Web.config file. In most cases, you will need to restart your application. In fact, if you change the Web.config or Global.asax file, or add to the Bin folder or the App_Code folder, the application is automatically restarted.

Figure 1 Modify the page without access to the source code

Method Realize
Accessing the control tree HTTP Module
Modify page base class Web.config
Control substitution Web.config
Build provider Assembly
redirect page HTTP handlers
Rewrite page HTTP handler or URL rewrite

Related Article

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.