In. NET Framework 4Microsoft launched the Route mechanism. This mechanism is not only widely used in MVC, but can also be used in WebForm.
Like Contex. RewritePath (), the Route function is also written in the Global. asax file.
1. Set the default page
The Code is as follows:
As shown in the code, this route redirects the website to the SimpleURLRewriting page by default.
After the actual access, the effect is as follows:
2. Configure Parameters
In actual projects, parameters are often used when we access the website. How should we configure parameters like this.
Global. asax:
Page background code:
It will get the parameter with the placeholder {parameter} in the URL, so you can access the following page through this rewrite rule:
However, for the following URL request, the parameter has no effect
, Although the parameter is not set in the position where the placeholder is {parameter}, the parameter item display is not empty.
This is because {parameter} is set with default parameters when routing is set.
3. Set a regular expression
In addition to rewriting the URL shown above, setting the default page and default parameters also allows the placeholder parameters to meet certain regular conditions.
The parameter must be a number according to the configured regular expression.
If not, the page cannot be found
4. PostBack data delivery
When setting the default page, we will find that the effect is different from that after using Contex. RewritePath,
After the routing function is used, the URL of the page is consistent with the URL in the address bar.
Therefore, in PostBack, you do not need to rewrite the Form like Contex. RewritePath.
Now let's take a look at the actual results:
Rewrite the rule code:
5. Link Processing
After the URL is rewritten, the access paths of the relative links, images, and CSS may be incorrect.
For example, in EN/SimpleURLRewriting. aspx
When accessing the page
Therefore, if the relative path is incorrect due to URL rewriting, it is best to specify the absolute path address to ensure that the image and link point to the correct URL.