Autumn Park QBlog resolve technical principles: UrlRewrite principle of no URL suffix (C)

Review article:

 

In this section, from the autumn garden of the entire station entrance: UrlRewrite.dll start parsing the principle of no suffix.

 

In view of the garden there are a lot UrlRewrite article, so do not explain the somewhat simpler:

 

1: The ISAPI Filter: namely online third-party publisher of the more common, such as: RewriteEval.dll

 

Autumn Park earlier version began in 2007, it is the use of third-party plug-ins, with the end of start-up companies, was silent for two years.

 

Back then participate in the development blog There are so few roles:

 

1: Technical Director is responsible for directing me to write CYQ.Data , and guide the other two players and plug-js Chinese Pinyin, and wrote a page base class

 

2: I am responsible for blog users the background, another colleague responsible for writing the blog front desk

 

3: a female colleague responsible for writing plug-js

 

4: two artists responsible for writing several sets of templates

 

5: Another colleague had a toss of Chinese Pinyin.

 

6: It had taken him three months or so, the development of it.

 

Seven people before doing live, now, a person I did, I know how easy it is not.

 

2009, with the CYQ.Data secondary small improvements, and plan to build a personal blog , by the way it replace the data layer, but found that it is not moving change, a business class on a million lines of code.

Colleagues that "copy + paste" powerful, performance is quite vividly, and therefore had to abandon the changes, not only to remove some of the features, and then reposition the next URL, simply transformed into single-user blog .

 

Also broke a space domain 51xxdn, no good cheap goods, 150 / year would not have opened the server every few days, all day long, and customer service is communication. After the expiration of one year after the site I threw, even off the record nor the domain name.

 

The year when using third-party plug-ins, trouble place:

Dll need to be submitted to the customer, then the customer service need to teach technicians how to add, the most frightening thing is also to restart IIS to take effect.

 

So many people put a server site, Zeneng casually asked to restart IIS to make it? But do not restart your site and they can not open, so every time should not pick the lunch time reboot, or not, why not toss?

 

After a technical customer service said the line, barely run up, but later found that the server is still quite frequent restart IIS, because the site is always open.

 

When there are code changes or add URL rules every change, they have to ask someone to restart under IIS, alas, the toss, it was also frustrating.

The new version of Autumn Garden , in order to avoid this, so naturally straining to consider it knocked off, then rewrite or myself.

 

Two: IHttpModule method of RewritePath

 

Myself, how come? Write a C ++ ISAPI will not? A: No.

 

Or honest with .net written.

Then to a simple point: toss from Global.asax to IHttpHandle, toss a half, wrong direction, and turn still in place, and later by the so a search, only to find, ways have to go IHttpModule to go on.

 

IHttpModule very easy to use, many people are using this toss, write a class inherits from IHttpModule, then the configuration file to register click on it, for example:

 

1: The New Class Library project: named: UrlRewrite

 

2: add a reference to System.Web, because IHttpModule under this name space, the default library is not referenced in this

 

3: The Class1.cs renamed UrlRewrite.cs, and let the class inherits from IHttpModule, implement interfaces, and ultimately as follows:

We put the little adjustments to the code such as:

#region IHttpModule members

public void Dispose()
{
//throw new Exception("The method or operation is not implemented.");
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

context_BeginRequest void (SENDER Object, EventArgs E)
{
the HttpApplication App = (the HttpApplication) SENDER;
HttpUrlRewrite (app.Context);
}
public void HttpUrlRewrite (the HttpContext context)
{
String URL = context.Request.Url.ToString (); // User request Url
// - Url here do a lot of logic
context.RewritePath ( "~ / Default.aspx", null, "url =" + url);

}
#endregion

The above code, meaning:

The entire request to the url parameters are as Default.aspx page, all requests are converted to Default.aspx? Url = www.cyqdata.com similar fashion

 

Here to build a site to accept and output

 

4: Adding a new Web site named: UrlRewriteDemo

ps: supposed to build the application, fear not open when everyone download the sample, it is an example of a website form

 

5: Add a reference to the project UrlRewrite

 

6: F5 momentarily to recall the web.config, and then add the code configuration items in the configuration file as follows:

< httpModules>
< add name="UrlRewrite" type="UrlRewrite.UrlRewrite,UrlRewrite" />
< /httpModules>

Present case of the example shown in FIG:

Then to write one line of code Default.aspx outputs the received parameters:

protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request["url"]);
}

 

Everything is ready to run, we see the results:

 

Figure 1: the page request, the default output is present, showing normal

 

Figure 2: a random requested page does not exist, the output result also indicates normal

 

Figure 3: a request even without the suffixes Url, outputs the result, he expressed little surprise

 

Seen from Figure 3, it seems that VS2005 integrated IIS to process all requests are unified, so even without the suffix can also be processed

 

Highlights:

For the ultimate in IIS site development or deployment, the default process is not aspnet_isapi.dll no suffix or suffixes such as pictures, therefore, we need to add extensions "universal map" to achieve this function.

 

See how to add under IIS6: How to install deploy Autumn Park CYQBlog site

As can be seen from the above parsed content, it can be taken to have any user output request address, including no suffix.

Autumn Park then specifically how to deal with the request, reads the configuration file starting a regular resolved or that other?

 

See you next resolve internal realization of the principle, so stay tuned.

 

Finally, the sample code download: UrlWrwriteDemo.rar

Reproduced in: https: //my.oschina.net/secyaher/blog/274295

Guess you like

Origin blog.csdn.net/weixin_34357436/article/details/91967011