Alternative dynamic load user controls

Source: Internet
Author: User

In the BS architecture, we usually build a Web site, often to create a website site, or to create a webapplication. Then create the corresponding ASPX file in the Web project, and the user control, custom controls, and so on. But as a new addition to this project, you have to modify the

A feature template, then my general program is this:

First: According to the new requirements, find the relevant pages in the project, for example, the corresponding page URL is index.aspx. At this point you can do the initial understanding of the original page.

Second: Analyze the code.

Third: Modify the code according to the new requirements.

I think this is a general friend of the mode of thinking. But this process is not uniform, as for how to change, I would like to discuss the following with the park friends how to achieve the requirements?

There may be multiple versions of the site, such as the minimum Chinese-English version, and the content presentation style of each version is slightly different.

I have enumerated three measures which I have come to my mind:

The first method: Each version of a separate site. Specific requirements for concrete implementation. The disadvantage of this approach is that maintenance is difficult and costly.

The second approach is to build a site that creates separate pages for each version of the same feature page. For example, a page that displays news, such as Chinese news, ASPX, English news, aspx, and so on. The disadvantages of this method are the same, but it is only a little easier.

The third way: to build a site, a function to build a page, but for different versions of different user controls, according to the corresponding parameters for dynamic load control. This approach is optimal. The disadvantage of this approach is that the coupling of the program is too strong, if you want to add a version, then you need to modify the function page.

In fact, the third method above can generally solve the problem. It was later found in new projects that HttpHandler applications can be customized to capture HTTP requests. A new solution was found.

IHttpHandlerFactory is more useful in. Net. It can preprocess the hander of the current page before the hander object is generated. In general, if we do URL rewrite, also will use HttpHandler, now can use it to achieve my above requirements.

Site, Just build a page webform2.aspx. In this page, the user control is dynamically loaded according to the URL of the request. This dynamic loader is not written in the Pageload event in WebForm2. Instead, it is written in the AddParsedSubObject event of the page. When a user accesses another page , such as a search page search.aspx, this page does not actually exist in the station. Instead, you use HttpHandler to capture this HTTP request, directed to an already existing webform2.aspx page, Completes loading of the search control in the AddParsedSubObject event in the Webform2.aspx page.

First: How do you come from a definition to capture HTTP requests?

1: Create a class that implements the interface IHttpHandlerFactory.

The IHttpHandlerFactory type exposes the following members.

Method:

GetHandler returns an instance of the class that implements the IHttpHandler interface.

Releasehandler enables a factory to reuse an existing handler instance.

Code

public class Webpagehandlerfactory:ihttphandlerfactory
{
Public virtual IHttpHandler GetHandler (HttpContext context, string RequestType, string URL, string path)
{
if (isexistedpage (path))
{
return pageparser.getcompiledpageinstance (URL, p Ath, context);
}
if (!path. ToLower ().        EndsWith (". aspx"))
{
Path = path.combine (path, "webform2.aspx");
The
URL = replaceurl (URL);
Path = replaceurl (path);
return pageparser.getcompiledpageinstance (URL, path, context);
}
/**////<summary>
///determine if the URL of the current request is real
///</summary>
///< param name= "path" > The relative path of the request URL </param>
///<returns></returns>
private bool Isexisted Page (string path)
{
//The first page is an existing
if path. ToLower (). IndexOf ("webform2.aspx") > 0)
{
ReTurn true;     
}
return false;

/**////<summary>
///Converts a page name that does not actually exist into a default.aspx
///that is already real, for example: default2.aspx does not actually exist, the Def Ault2.aspx converted to Default.aspx
///</summary>
///<param name= "Input" > currently requested URLs that do not actually exist, for example: default      2.aspx</param>
///<returns></returns>
Private string Replaceurl (string input)
       {
//Regex search and replace
regexoptions options = Regexoptions.none
Regex regex = new Regex (@ "w+.aspx", options);
String replacement = @ "webform2.aspx";
String result = Regex. Replace (input, replacement);
return result;
}
Public virtual void Releasehandler (IHttpHandler handler)
{
}
}

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.