Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

URL Encoding Library / class whatever ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Just Me - 17 Sep 2007 10:26 GMT
OK,  this is sort of a two part question here.

The scencario is that I am building an article.aspx page which takes a
parameter ( ArticleID ) and this represents an article in the database. This
all works fine and as its a public site, google has index it.

Now I read somewhere that adding extra information in the url might give the
document score on google extra points so I thought, ok why not just add some
hyphen seperated keywords to the parameter and simply strip those off when
the articel.aspx loads, for example. Article.aspx
?ArticleID=9119_asp.net-2.0-xml-datagrid

So the code inside discards everything from the '_' onwards and uses the
numeric value to lookup the article. OK thats the easy bit. Because this is
a url, Im assuming I cant use special characters like '.' '#'' etc etc as
they have special meaning in the url.

1.) So Is the special character issue also applied to query strings, in
other words, should they also be escaped as well. ?

2.) Does anyone have code like a set of regexes expressions or something
which will automatically encode the special characters, what I guess Im
looking for is something  which will turn this for example

Article.aspx?ArticleID=9999_asp.net, visual studio, xml,

Into

Article.aspx?ArticleID=9999_asp%nn-net-visual-studio-xml

Any help would be apprciated
Mark Rae [MVP] - 17 Sep 2007 10:36 GMT
> 1.) So Is the special character issue also applied to query strings, in
> other words, should they also be escaped as well. ?

Yes, though in this particular scenario you're essentially discarding the
part of the querystring which might have caused you problems... However,
it's good practice to do the encoding anyway...

> 2.) Does anyone have code like a set of regexes expressions or something
> which will automatically encode the special characters, what I guess Im
> looking for is something  which will turn this for example

No need for RegEx - Server.URLEncode is all you need:
http://www.4guysfromrolla.com/webtech/042601-1.shtml

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Just Me - 17 Sep 2007 11:58 GMT
Excellent answer for web based projects.

It seems you cant get to this functionality from a windwos based app. I cant
seem to instantiate a httpserverutility class or make use of its functions.

Server is an instrinic instance of this class in an asp.net web app, do you
know if there is a way to instantiate it in a windows app, it shows an
example of doing so, but without the new keyword, and of course you cant use
that.

>> 1.) So Is the special character issue also applied to query strings, in
>> other words, should they also be escaped as well. ?
[quoted text clipped - 9 lines]
> No need for RegEx - Server.URLEncode is all you need:
> http://www.4guysfromrolla.com/webtech/042601-1.shtml
Mark Rae [MVP] - 17 Sep 2007 12:45 GMT
> Excellent answer for web based projects.

This is an ASP.NET newsgroup... :-)

> It seems you cant get to this functionality from a windwos based app. I
> cant seem to instantiate a httpserverutility class or make use of its
> functions.

1) Add System.Web as a reference in your WinForms project

2) using System.Web;

3) HttpUtility.UrlEncode(...)

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Just Me - 17 Sep 2007 13:17 GMT
Thanks mark,

Im aware what the newsgroup was, it was just that I was testing it in a
quicker environment. And I had already added a reference and could see the
class in the namespace, but was unable to instantiate it, or use it as a
static method.

You try it and see if you can make it work, cos I cant !

:)

>> Excellent answer for web based projects.
>
[quoted text clipped - 9 lines]
>
> 3) HttpUtility.UrlEncode(...)
Mark Rae [MVP] - 17 Sep 2007 13:35 GMT
> Thanks mark,
>
[quoted text clipped - 4 lines]
>
> You try it and see if you can make it work, cos I cant !

Working fine for me:

string strRawQS = "ArticleID=9119_asp.net-2.0-xml-datagrid";
string strEncodedQS = System.Web.HttpUtility.UrlEncode(strRawURL);

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Just Me - 17 Sep 2007 13:55 GMT
Odd, I was using vb rather than csharp, although that should not have made a
difference, could you be so kind as to try it in vb for me

Thanks

>> Thanks mark,
>>
[quoted text clipped - 9 lines]
> string strRawQS = "ArticleID=9119_asp.net-2.0-xml-datagrid";
> string strEncodedQS = System.Web.HttpUtility.UrlEncode(strRawURL);
Mark Rae [MVP] - 17 Sep 2007 14:03 GMT
> Odd, I was using vb rather than csharp, although that should not have made
> a difference, could you be so kind as to try it in vb for me

Works perfectly:

Dim strRawQS As String = "ArticleID=9119_asp.net-2.0-xml-datagrid"
Dim strEncodedQS As String = System.Web.HttpUtility.UrlEncode(strRawQS)

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Patrice - 17 Sep 2007 14:47 GMT
Note that this is a shared methode ie. you don't need to create an instance
of this class.
If for some reason it's still doesn"t work your best bet is to describe what
you have (i.e. the exact error message and the offending line of code).

--
Patrice

>> Odd, I was using vb rather than csharp, although that should not have
>> made a difference, could you be so kind as to try it in vb for me
[quoted text clipped - 3 lines]
> Dim strRawQS As String = "ArticleID=9119_asp.net-2.0-xml-datagrid"
> Dim strEncodedQS As String = System.Web.HttpUtility.UrlEncode(strRawQS)
Just Me - 17 Sep 2007 15:39 GMT
Must be me then, ill try it again
Cheers

>> Odd, I was using vb rather than csharp, although that should not have
>> made a difference, could you be so kind as to try it in vb for me
[quoted text clipped - 3 lines]
> Dim strRawQS As String = "ArticleID=9119_asp.net-2.0-xml-datagrid"
> Dim strEncodedQS As String = System.Web.HttpUtility.UrlEncode(strRawQS)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.