5

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.

3
  • 4
    Why the close vote? The question is quite clear. Commented Jan 15, 2013 at 11:39
  • Which ASP.NET version? Routing is supported much better in V4+ Commented Jan 15, 2013 at 11:42
  • stackoverflow.com/questions/10659199/… Commented Jan 15, 2013 at 11:48

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Apparently this does not work with User controls? is there anyway I can get this work on user controls
You can use HttpContext.Current.Request.RequestContext.RouteData to get the route data from anywhere within your application.
2

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

Comments

1

You could try using QueryStrings.

Redirect your page like this:

Response.Redirect("Webform2.aspx?Name=" +
this.txtName.Text + "&LastName=" +
this.txtLastName.Text);

And on your new page get the values like this:

private void Page_Load(object sender, System.EventArgs e)
{
    this.txtBox1.Text = Request.QueryString["Name"];
    this.txtBox2.Text = Request.QueryString["LastName"];
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.