asp.net urlrouting
时间: 2023-09-03 10:05:29 浏览: 111
ASP.NET URL Routing is a feature in ASP.NET that allows you to define URL patterns and map them to handler classes or files. This allows you to create clean, user-friendly URLs for your web application.
To enable URL Routing in ASP.NET, you need to do the following:
1. Add the following code to your web.config file:
```
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
```
2. Define your URL patterns in the Global.asax file using the `MapPageRoute` method. For example:
```
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Products", "products/{category}/{productName}", "~/Products.aspx");
}
```
In this example, we're defining a URL pattern that matches any URL that starts with "/products/" followed by a category name and product name. When this URL is requested, it will be handled by the Products.aspx page.
3. Access the route data in your handler class or file using the `RouteData` property. For example:
```
string category = RouteData.Values["category"] as string;
string productName = RouteData.Values["productName"] as string;
```
This will retrieve the values of the "category" and "productName" parameters from the URL.
Overall, ASP.NET URL Routing is a powerful feature that can help you create more user-friendly and search engine-friendly URLs for your web application.
阅读全文