Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters ASP.Net

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.

like image 388
ravisilva Avatar asked Jan 15 '13 11:01

ravisilva


People also ask

How pass multiple parameters in URL in asp net?

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.

What is difference between FromQuery and FromBody?

[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.

What is parameter in asp net?

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.


2 Answers

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.

like image 133
Rudi Visser Avatar answered Sep 19 '22 17:09

Rudi Visser


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

like image 25
Branimir Avatar answered Sep 19 '22 17:09

Branimir