An user might want to email an url like the one given below:
http://gitolekha:82/default.aspx?ConnectSearch=good sharepoint blog

We need to do url encoding to send such links because often url’s contain characters outside the ASCII set, so URL encoding converts the url to valid ASCII sets.URL encoding replaces unsafe ASCII characters with “%” followed by two hexadecimal digits corresponding to the character values in the ISO-8859-1 character-set.

URLs cannot contain spaces. URL encoding normally replaces a space with a + sign or as %20.

To do url encoding with .NET,one usually uses System.WebHttpUtility.UrlEncode to encode the querystring.

I had to do this through javascript.There are methods like
escape(string) to do encoding, but it did not work for me.
So, what I chose is the replace mothod to replace all unsafe characters.
what I used is regular expression to replace all occurences of space in  string “good sharepoint blog” as encodestring.replace(/\\s+/g ,’$-*’);

A detailed explaination of the above is as follows,
/ = start pattern
, = pattern to look for in the string badtext
/ = end pattern
g = global (anywhere the pattern match occurs in the string)
“$-*” = what to replace the pattern with if found

So, the full code to send the encoded url in email is given below.

parent.location = ‘mailto:?body=” + HttpUtility.UrlEncode(SPContext.Current.Web.Url + “/default.aspx?ConnectSearch=”) + “‘+txtSearch.value.replace(/\\s+/g ,’$-*’)+’~-~User=’+txtTopicsSearch.value.replace(/\\s+/g ,’$-*’)+’~-~Place=’+txtDocTypeSearch.value.replace(/\\s+/g ,’$-*’)+’~-~Country=’+txtCountrySearch.value.replace(/\\s+/g ,’$-*’)+’&subject=site search link’

The mail has a subject line of ‘site search link’ as seen above.

This emails the encoded url as,
http://gitolekha:82/default.aspx?ConnectSearch=good$-*sharepoint$-*blog~-~User=gitolekha~-~Place=any$-*where/$-*in$-*India~-~Country=All$-*geographic$-*scopes

Now when the user types/paste the above url in the browser, we can use httphandler to make the application decode the above url in the desired format.
Now, we know that an ASP.NET HTTP handler is the process that runs in response to a request that is made to an ASP.NET Web application. When users request an .aspx file, the request is processed by the page handler.An IHttpModule is registered in the Web.Config file and it is notified of every HTTP request.
IHttpModule is an interface defined in System.Web.dll. The interface contains only two method signatures: Init and Dispose. On the Init method wire up, the behavior—for example application events—that you want the IHttpModule to support and perform any clean up in the Dispose method.

using System.Web;
namespace Gitolekha.Sample.KnowledgeManagement
{
class CustomHandler : IHttpModule
{
public void Dispose()
{
//throw new Exception(“The method or operation is not implemented.”);
}
public void Init(HttpApplication context)
{
context.BeginRequest+=new EventHandler(context_BeginRequest);
}
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string requestedPage = app.Request.RawUrl;
if (requestedPage.Contains(“?ConnectSearch=”))
{
string strurl = app.Request.Url.AbsoluteUri;
if (strurl .Contains(“$-*”))
{
strurl = strurl.Replace(“$-*”, ” “);
}
if (strurl.Contains(“~-~”))
{
strurl = strurl.Replace(“~-~”, “&”);
}
string strencode = HttpUtility.UrlEncode(strurl );
HttpContext.Current.Response.Redirect(strurl, true);
}}

This will do the required decoding for this url.
Possible scenarios/reasons for users wanting to send url might be Sometimes it is easy for users to email interesting links of a site to another user.To elaborate, suppose you have a document management system where the users like to share interesting and important links of documents to other users through emails.

Advertisements