jame
jame

Reputation:

How do I encode an URL?

When I run my project I get the url http://localhost:5973/PageToPageValuePass/Default.aspx I want to Encode the URL since sometimes I need to transfer data from page to page. When the urls are encoded then it increases the reliability.

Server.UrlEncode("http://www.google.com/c#");

I get this, but how do I use it to help me encode the url?

Upvotes: 11

Views: 31647

Answers (4)

RRUZ
RRUZ

Reputation: 136391

try this

  • in ASP.NET
    Server.UrlEncode("http://www.google.com/c#");
  • in WinForms using System.Web.dll

    HttpUtility.UrlEncode("http://www.google.com/c#");

Upvotes: 8

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

say you want to create a link with some parameters you can use it as follows:

aspx:

Click Here

code behind:

myLink.Href = Page.ResolveClientUrl("~/MyPage.aspx") + "?id=" + 
   Server.UrlEncode("put here what ever you want to url encode"); 

Or as in your question:

myLink.Href = "http://www.google.com/")+Server.UrlEncode("C#");

this will put in html:

<a id="myLink" runat="server" target="_self" href="http://www.google.com/c+c%23">

Upvotes: 0

csharptest.net
csharptest.net

Reputation: 64218

If your encoding parts of the path:

System.Uri.EscapeUriString("c#")

If your encoding 'arguments':

String.Format( "http://something/?test={0}", System.Uri.EscapeDataString("c#") );

Upvotes: 35

Stuart Thompson
Stuart Thompson

Reputation: 1859

Url encoding is used to ensure that special symbols included in a url (most likely in a querystring) are not mistakenly interpreted as those used in the parsing and processing of a url. For example, the + symbol is used to indicate a space in a url. However, if you were intending for a + symbol to be a part of your querystring then you would want to encode that querystring before sending it to a browser.

For example. Imagine you have written a page that receives a math equation on the querystring and displays that equation on the page.

The url might be: http://yoursite.com/displayMath.aspx?equation=3+5

The + symbol in this case is intended to be a meaningful part of the equation. However, without a UrlEncode it would be interpreted as representing a space. Reading this value from the querystring on the receiving page would yield "3 5", which is not what was intended.

Instead of redirecting to that url directly, you would want to URL encode the request first. You might write the following code:

string equation = "3+5";
string url = String.Format(@"http://yoursite.com/displayMath.aspx?equation={0}", equation);
string encodedUrl = Server.UrlEncode(url);
Response.Redirect(encodedUrl);

This would ensure that a subsequent Request.Querystring["equation"] would receive the equation intact because any special symbols would first be encoded.

I'm not sure I understand your use case for encoding urls. If you could perhaps provide more information on what you are trying to achieve I will attempt to answer more fully. For now I hope that this information is useful.

Upvotes: 3

Related Questions