[C #] Using Route for URL rewriting

Source: Internet
Author: User

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.

 

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.