Ii. Understand the operating mechanism of ASP. NET (for example, URL rewriting Based on httphandler)

Source: Internet
Author: User

URL rewriting is to put some articles like article. aspx? Path of ID = 28
Rewrite to article/28 /.

When the user accesses article/28/
We use Asp.net to redirect this request to article. aspx? Id = 28 path
There are two ways to do this.

I. httpmodule-based solution
This solution has some disadvantages. We will discuss the specific disadvantages later.
I once wrote an articleArticle《URL rewriting without components (applicable to larger projects)"
It is written in this mode.

Ii. httphandler-based solution
This example is based on this solution.
Let's take this example.

Iii. httphandlerfactory-based solution
As its name implies, This is a factory that can process requests based on different files.

Let's look at webconfig first, just like webconfig described in the previous section.

<? XML version = "1.0" ?>
< Configuration >
< System. Web >
< Compilation Debug = "True" > </ Compilation >
< Httphandlers >
< Add Verb = "*" Path = "*. Jsp" Type = "Xland. myhandler"   />
</ Httphandlers >
</ System. Web >
</ Configuration >  

Verb indicates one or more of the permitted actions "get", "Post", and "put". The asterisk "*" indicates that all allow
Path allows access to files with the JSP extension
Type specifies the httphandler Processing Method

The following describes the myhandler method.

Using System;
Using System. Collections. Generic;
Using System. Web; // Reference a web namespace
Using System. text;
  Namespace Xland
{
Public   Class Myhandler: ihttphandler // Inherit ihttphandler
{
Public   Void Processrequest (httpcontext context) // Implementation Interface Method
{
String Path = Context. Request. url. pathandquery;
Int Startnum = Path. lastindexof ( ' / ' ) +   1 ;
String Name = Path. substring (startnum );
String [] Parames = Name. Split ( ' - ' );
String Truename =   " / " + Parames [ 0 ] + " . Aspx " + " ? Id = " + Parames [ 1 ]. Replace ( " . Jsp " , "" );
Context. server. Execute (truename );
}
Public   Bool Isreusable // Implementation Interface Method
{
Get { Return   False ;}
}
}
}

My intention is to put a user request like article-49.jsp
Convert to article. aspx? Id = 49
The last sentence is to execute the specified page processing.Program

See article. aspx.

Using System;
Using System. collections;
Using System. configuration;
Using System. DaTa;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Namespace _ 1
{
Public   Partial   Class Article: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Response. Write ( " The received ID is "   + Request [ " ID " ]);
}
Protected   Void Button#click ( Object Sender, eventargs E)
{
Response. Write ( " Return successful " );
}
}
}

I made the PostBack event of the button on the page. It can be successfully executed here.

< Httphandlers >
< Add Verb = "*" Path = "*. Jsp" Type = "Xland. myhandler"   />
</ Httphandlers >

The aboveCodeIs to process all. jsp extension files
You can write

< Httphandlers >
  < Add Verb = "*" Path = "* 123.jsp" Type = "System. Web. UI. pagehandlerfactory" Validate = "True"   />
< Add Verb = "*" Path = "*. Jsp" Type = "Xland. myhandler"   />
</ Httphandlers >  

Returning a class of files to Asp.net for processing
Then, you can register ihttphandlerfactory to process the part you want to process.

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.