Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use URL Routing in ASP.NET4

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use URL Routing in ASP.NET4". In daily operation, I believe many people have doubts about how to use URL Routing in ASP.NET4. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use URL Routing in ASP.NET4". Next, please follow the editor to study!

What is URL Routing?

First of all, URL routing was actually introduced in ASP.NET 5 SP1, but it hasn't been used before, and many Microsoft introductions to ASP.NET 4 regard this as a new feature, so let's take this as the title.

Previously, the URL of a typical ASP.NET was usually as follows: http://www.myexample.com/salesreport.aspx?year=2009

The salesreport.aspx in this URL represents a real physical file, and the existence of the suffix .aspx in url is not only completely meaningless, but also makes the url not SEO-friendly. After using URL Routing, we can use the following more concise address to access, http://www.myexample.com/salesrepot/2009

Using Route Engine for URL Mapping

In asp.net mvc, url is mapped to the corresponding controller and action through MapRoute, while in web form, in Global.assx 's Application_Start, we map url to a page through MapPageRoute

Void RegisterRoutes (RouteCollection routes) {routes.MapPageRoute ("SalesRoute", "SalesReport/ {year}", "~ / sales.aspx");}

MapPageRoute uses three parameters, * is the name of the Route, the second is the mapping Pattern of the URL, and * is the corresponding ASPX page. In addition to the most commonly used direct method, you can also use other methods of its overload to set the default value by route and add various constraints, such as the following one

Void RegisterRoutes (RouteCollection routes) {routes.MapPageRoute ("SalesRoute", "SalesRoute/ {year}", "~ / sales.aspx", true, new RouteValueDictionary {{"year", DateTime.Now.Year.ToString ()}}) New RouteValueDictionary {{"year", @ "\ d {4}"}) }

Compared to * * route, this has three more parameters. * are Boolean values, the last two are RouteValueDictionary, of which * specify a default value, and * one is a constraint that constrains the parameter year must be a four-digit number through a regular expression.

After successfully mapping SalesRoute/2009 to a sales.aspx page, how do you get this "2009" value in sales's code? Get through the RouteData of the Page class

Protected void Page_Load (object sender, EventArgs e) {string year = RouteData.Values ["year"] as string;}

Use Routing Engine to generate URL

In addition to parsing URL, we can also generate these concise URL, such as the following code

RouteValueDictionary parameters = new RouteValueDictionary {{"year", "2008"}, {"category", "recreation"}}; VirtualPathData vpd = RouteTable.Routes.GetVirtualPath (null, "SalesRoute", parameters); hyperLnk.NavigateUrl = vpd.VirtualPath

The SalesRoute here is the Route we registered in Application_Start. It's worth noting that we don't have "{category}" in SalesRoute's Pattern, so what does RouteEngine do with this value? At this point, category is added to the url as a querystring, and the generated URL will look like this: http://www.myexample.com/salesreport/2009?category=recreation

In addition, if you add a button to the sales page, will the corresponding url of the button be as concise as the url above? The answer is yes. However, the postback of the page is achieved by specifying Action in the Form tag.

At this point, the study on "how to use URL Routing in ASP.NET4" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report