Instructions on how to use Url Routing in WebForm

Source: Internet
Author: User
Today, I saw a question about the idea of using System. we. Routing in webForm to rewrite the url. It took some time to sort it out and answer the following question; To understand how to use url Routing, first clarify the following issues:

What is URL Routing?

The so-called URL Routing (URL Routing) means that in the Web, the URL is no longer directed to a physical file, but a string that describes the URL Routing, developers can customize the format of this string. By default, URL Routing can be directly used in ASP. NET applications, but some configuration is required on the ASP. NET site for use.

Why use URL Routing?

Before using URL Routing, could our URL be a http://www.website.com/Index.aspx? Id = 1. After URL Routing is used, our URL can be changed to http://www.website.com/index/1. The modified URL is more friendly and SEO-friendly.

Can URL Routing be used only in MVC?

The Routing assembly (System. Web. Routing. dll) is included in. NET Framework V3.5 sp1, and MVC is released later. Therefore, it cannot be said that URL Routing can only be used in MVC. However, in MVC, Routing is added to some extension methods (included in the RouteCollectionExtemsion class of System. Web. Mvc), which is more convenient to use.

The following describes how to use URL Routing in Web Form.


1. add reference to the assembly System. Web. Export actions. dll, System. Web. Routing. dll.

2. Add an IRouteHandler implementation class WebFormRouteHandler

The Code is as follows:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Routing;
Using System. Web. Compilation;
Using System. Web. UI;


/// <Summary>
/// Summary of WebFormRouteHandler
/// </Summary>
Public class WebFormRouteHandler: IRouteHandler
{
/// <Summary>
/// Specific page path
/// </Summary>
Public string VirtualPath
{
Get;


Private set;
}
Public WebFormRouteHandler (string virtualpath)
{
This. VirtualPath = virtualpath;
}
# Region IRouteHandler Member
/// <Summary>
/// Implement the GetHttpHandler method in the IHttpHandler interface,
/// </Summary>
/// <Param name = "requestContext"> </param>
/// <Returns> </returns>
Public IHttpHandler GetHttpHandler (RequestContext requestContext)
{
// Obtain the URL parameter from the ReqestContext object and write the value to the Items set of HttpContext for use on the route target page
Foreach (var urlParm in requestContext. RouteData. Values)
{
RequestContext. HttpContext. Items [urlParm. Key] = urlParm. Value;
}
// Get the Page Object for instantiation
Var page = BuildManager. CreateInstanceFromVirtualPath (VirtualPath, typeof (Page) as IHttpHandler;
Return page;
}


# Endregion
}


3. Modify the web. config file (the url Routing method is required)

<HttpModules>

<Add name = "UrlRoutingModule" type = "System. Web. Routing. UrlRoutingModule, System. Web. Routing, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
</HttpModules>

4. The Global. asax Page code is as follows (if not, add this file)

<% @ Application Language = "C #" %>
<% @ Import Namespace = "System. Web. Routing" %>
<Javascript runat = "server">

Public static void RegisterRoutes (RouteCollection routes)
{
// Note that the WebFormRouteHandler class is called here
Routes. Add ("Named", new Route ("NameIndex/{action}", new RouteValueDictionary {"action", "NameIndex" }}, new WebFormRouteHandler ("~ /Named/Index. aspx ")));
Routes. Add ("Numbers", new Route ("NumberIndex/{action}", new RouteValueDictionary {"action", "NumberIndex" }}, new WebFormRouteHandler ("~ /Numbers/Index. aspx ")));
// Add verification. The value of the action parameter must be 13 characters. If the length of the action parameter is not equal to 13 characters (change as appropriate ), the error message "The resource cannot be found" is displayed.
Routes. add ("Validate", new Route ("ValidateIndex/{action}", new RouteValueDictionary {"action", "ValidateIndex" }}, new RouteValueDictionary {"action ", @ "\ w {13}" }}, new WebFormRouteHandler ("~ /Validate/Index. aspx ")));

}


Void Application_Start (object sender, EventArgs e)
{
RegisterRoutes (System. Web. Routing. RouteTable. Routes );
}

Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed


}

Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs


}


Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts


}


Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session Mode
// If it is set to StateServer or SQLServer, this event is not triggered.


}
</Script>


1. Named (folder)

The following is a file: Index. aspx.

2. Numbers (folder)

The following is a file: Index. aspx.

3. Validate (folder)

The following is a file: Index. aspx.

Modify the Default. aspx file in the root directory of the project.

Add the following content:

<Div> <a href = "NameIndex"> Go To Named/Index </a> </div> <br/>
<Div> <a href = "NumberIndex"> Go To Numbers/Index </a> </div> <br/>
<Div> <a href = "ValidateIndex"> Go To ValidateIndex! </A> </div>
After all the code is written, debug the code below. If there is no problem, the program can run.

Finally, run the program and click the three links on the Default. aspx page to check the effect.


Note: The program contains the use of parameters, parameter verification, etc,

If you have any better suggestions for this program, please explain that if you think there is a problem, please do not hurt people, come out and mix, always have to pay back
If necessary, I will upload the source code. You are welcome to participate in the discussion;


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.