4b0
4b0

Reputation: 22323

Passing current page url as a parameter in url

I have a repeater.Insidea a repeater there is a button.On item Command I try

protected void rptPost_ItemCommand(object source, RepeaterCommandEventArgs e)
{
 int contentID = Int32.Parse(e.CommandArgument.ToString());//Its return 1
 string host = HttpContext.Current.Request.Url.ToString();//its return http://localhost:1377/Forum.aspx
 //string URL = "~/Modules/Forum/PostDetails.aspx?ID=" + contentID +"&BackUrl=" + host;//Need help hear its not work
 string URL = "~/Modules/Forum/PostDetails.aspx?ID="  + contentID ;//Its work
 Response.Redirect(URL); 

}

when I only try to pass ID its work but if I try to pass multipal url one is ID and next is Current Page Url its unable to find page.And gives page not found error.I unable to understand the problem.Passing Url as a parameter is not support or I missing some thing.Thanks.

Upvotes: 2

Views: 6371

Answers (1)

Paul Keister
Paul Keister

Reputation: 13097

Here is the solution:

host = HttpUtility.UrlEncode(host)

and here is why it works:

You are passing the host URL as a GET parameter. GET parameters are part of the GET URI for the redirect. URI syntax includes characters with special meanings, in particular you are using colon (:) and slash (/), which have special meaning in a URI string. In almost all cases special characters should not be used as part of your GET parameter data values.

For cases such as yours, where there is a need to include special characters, UrlEncoding was developed. UrlEncoding, or Percent-encoding, substitutes special characters in URIs with values encoded using the percent sign. These escape sequences are automatically converted back to the original special character as part of URI query processing. The HttpUtility class provides the UrlEncode method as a convenient way to escape all special characters in an input string.

Please note that it is a good idea to URL encode all data that you pass via GET parameters unless you have full control over the data being passed and you can guarantee that encoding is not necessary. This can be a security issue because attackers can include new parameters and otherwise manipulate your URI if you pass user input to a GET parameter without first URL encoding it.

Upvotes: 7

Related Questions