Is there any way that I can pass parameters to a user control/page through the path(URL) in a ASP.Net Web application(not MVC).
i.e.
http://abc.com/news/para-1/para-2
where para-1
and para-2
are parameters.
To pass multiple parameters, we will use “&” symbol to separate the other field and value combinations. On button click (from code behind), redirect to another page with two QueryString parameters. Now this example has two parameters or variables. The first is the userid=43 and second is the contentid=9 respectively.
[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.
The Parameter class represents a parameter in a parameterized SQL query, a filtering expression, or a business object method call that an ASP.NET data source control uses to select, filter, or modify data. Parameter objects are contained in a ParameterCollection object.
What you are looking for is called Routing
.
If you're using .NET 4+, you can read how to implement it in a Web Forms application on MSDN.
Your rule essentially comes down to this, assuming news.aspx
is where you want to go:
routes.MapPageRoute("NewsRoute",
"News/{arg1}/{arg2}",
"~/news.aspx");
You can then proceed to access the values using any of the following methods:
Page.RouteData.Values["arg1"]
<asp:Literal ID="Literal" Text="<%$RouteValue:arg1%>" runat="server"></asp:Literal>
If you're not using .NET 4+, Scott Hanselman writes about ASP.NET FriendlyUrls, which is available in NuGet.
You can use ASP.NET Friendly URLs
The ASP.NET Friendly URLs library makes it easy to enable extensionless URLs for file-based handlers (e.g. ASPX, ASHX) in ASP.NET applications.
There is a good introduction by Scott Hanselman: Introducing ASP.NET FriendlyUrls - cleaner URLs, easier Routing, and Mobile Views for ASP.NET Web Forms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With