⬆️ ⬇️

URL routing in ASP.NET 4 Web Forms

image What is URL routing?



We first introduced URL routing in ASP.NET 3.5 SP1, which is now used in ASP.NET MVC applications to provide clean, SEO-friendly, “web 2.0” URLs. URL routing allows you to configure the application to accept requested addresses that do not correspond to physical files. You can use routing to advertise URLs that are semantically meaningful for users and that can help search engine optimization (SEO).



For example, here is the URL of a traditional page that displays product categories:

www.mysite.com/products.aspx?category=software


Using URL routing in ASP.NET 4, you can configure the application to accept the following URL to display the same information:

www.mysite.com/products/software


With ASP.NET 4.0, URLs, as shown above, can be tied to ASP.NET MVC controller classes and simple ASP.NET Web Forms pages.



Binding URLs using ASP.NET MVC

')

Routing URL engine submitted with ASP.NET 3.5. SP1 provides a powerful way to capture incoming URLs. You write code that is part of downloading an application to register / bind URLs that match a specific format for writing handlers.



Below is an example of how you can use ASP.NET MVC today to link / products / software URL to the “Products” controller class, which has a method called “Browse”:



image



Binding URLs using ASP.NET Web Forms



ASP.NET 4.0 also allows you to use the URL routing engine to bind URL addresses to ASP.NET Web Forms pages.



Below is an example of how you can use the MapPageRoute () helper method in ASP.NET 4.0 to link the / products / software URL to the “Products.aspx” page, which is directly in the root directory:



image



The first two parameters of the MapPageRoute () helper are exactly the same as those of MapRoute (). The first parameter specifies the friendly name of the route, the second defines the format of the URL address. The third parameter tells the Products.aspx page to handle the URL instead of the controller class. You can optionally define additional parameters for MapPageRoute () that are used as “route restrictions” and provide “default values ​​for parameters”, as is possible with ASP.NET MVC when registering a route.



On the Products.aspx page, you can write code as shown below, which uses the new ASP.NET 4.0 property Page.RouteData to get the value of the “category” parameter bound using the URL filter / products / {category}, which connects the category of products to display :



image



In addition, for programmatic access to the incoming parameters of the router using the above code, you can use the new declarative <asp: routeparameter> control with any ASP.NET DataSource control for declarative communication with a route value. For example, below, we use <asp: routeparameter> to bind the @category select parameter of the request, with the / products / {category} parameter in the router URL:



image



Getting URLs in ASP.NET Web Form



The URL address routing engine in ASP.NET can use in both cases: binding the incoming URL to handlers in the code and programmatically generating outgoing URLs using the same binding registration logic.



For example, above, when we linked the / product / {category} URL, we gave it a friendly name - “products-browse”. This allows us to use the new helper method Page.GetRouteUrl () to search for a route in the URL addressing system, optionally define parameters for it, and then return the real URL



For example, the code below, returns the value of the URL “/ products / software”



image



You can access a helper method in the code-behind file or directly in the .aspx file.



Also, a set of Response.RedirectToRoute () methods appeared that can be used to redirect users along a route (no matter where to use MVC or Web Forms) and optionally pass parameters to it.



Processing PostBack Scripts



URL routing in ASP.NET 4.0 fully supports postback scripts. The <form runat = ”server”> control will automatically substitute the URL that generated the page. For example, if you open a page with the address / products / software, any server control <form runat = ”server”> will generate <form action = ”/ products / software”> on the output - this means that any postback script that occurs on the page, saves the original URL.



All of this allows you to maintain clean, SEO-friendly, readable URLs in Web Forms and postback scripts, and avoids the cunning tricks that are now constantly used in URL rewriting modules to achieve some of the features.

Source: https://habr.com/ru/post/73207/



All Articles