We change URL'? hurriedly
One of most slaboosvehhjonnykh - but potentially useful - methods .NET Framework regarding, concerning ASP.NET, is the method of class HttpContext RewritePath. This method is used ASP.NET for reception of the sessional identifier from URL'a, at the involved status of session without use kuki. Method RewritePath also can be used and for substitution URL'a inside the application. For demonstration we shall assume, that we have following ASPX-file which we shall name RewritePath.aspx:
<html>
<body>
<h2> <asp:Label ID = "Output" RunAt = "server"/> </h2>
</body>
</html>
<script language = " C * " runat = "server">
void Page_Load (Object sender, EventArgs e)
{
string id = Request. QueryString ["ID"];
if (id! = null ** id! = String. Empty) {
switch (id) {
case "1":
Output. Text = " Give me chastity and " +
" continence, but not yet. ";
break;
case "2":
Output. Text = " A programmer is a device " +
" for turning coffee into code. ";
break;
case "3":
Output. Text = " Blessed is the man who, " +
" having nothing to say, abstains from " +
" giving wordy evidence of the fact. ";
break;
}}}
</script>
If this page to place in virtual catalogue Foo the following three URL'a will display three different statements:
http: // .../foo/rewritepath.aspx? id=1
http: // .../foo/rewritepath.aspx? id=2
http: // .../foo/rewritepath.aspx? id=3
And now present, that you want to give to your users to receive these statements with use such 'phantom' URL'ov:
http://.../foo/quotes/page1.aspx
http://.../foo/quotes/page2.aspx
http://.../foo/quotes/page3.aspx
It can be achieved, having used there is some magic - hurriedly having transformed URL a kind .../quotes/page1.aspx in URL a kind .../rewritepath.aspx? id=1 - having used method HttpContext. RewritePath. Cunning will consist in gathering searches of a kind/quotes/page1.aspx,/quotes/page2.aspx of ASP.NET HTTP-channel and their transformation into search of a kind/rewritepath.aspx? id=1,/rewritepath.aspx? id=2 and t.d.. File Global.asax which also does{makes} it:
<script language=C * runat = "server">
void Application_BeginRequest (Object sender, EventArgs e)
{
// TODO: transformation of a way of a kind
//. ../quotes/page1.aspx in a way of a kind
//. ../rewritepath.aspx? id=1
//
HttpContext context = HttpContext. Current;
string oldpath = context. Request. Path. ToLower ();
string token = "/quotes/page";
int i = oldpath. IndexOf (token);
int len = token. Length;
if (i! =-1) {
int j = oldpath. IndexOf (".aspx");
if (j! =-1)
{string id = oldpath. Substring (i + len, j - (i + len));
string newpath = oldpath. Replace (token + id + ".aspx", "/rewritepath.aspx? id = " + id);
context. RewritePath (newpath);
}}}
</script>
Application_BeginRequest It is caused in the beginning of each search. In this realization there is an allocation of part URL'a (for example,/foo/quotes/page1.aspx), replacement of this part by the link by real page (for example,/foo/rewrite.aspx? id=1) and a call of method RewritePath for perenacelivanija search. Try the following: copy file RewritePath.aspx in the catalogue wwwroot on your web - server and try to type{collect} in an address bar of your browser the following:
http://localhost/quotes/page1.aspx
The mistake " page not found " will be generated. Now we shall copy Global.asax in wwwroot and we shall try once again. This time all will earn due to change URL'a in event Application_BeginRequest.
RewritePath it is used in many applications. For example, you can use this method for entering the pages having parameters in URL'e, in favorites. This method also can be used for concealment of real ways in your application from reasons of safety. Well and now, when you know, that method RewritePath exists, for certain can find to him and other application.

|