URL Encoding Library / class whatever ?

J

Just Me

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
 
M

Mark Rae [MVP]

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
 
J

Just Me

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.
 
M

Mark Rae [MVP]

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(...)
 
J

Just Me

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 !

:)
 
M

Mark Rae [MVP]

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 !

Working fine for me:

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

Just Me

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
 
M

Mark Rae [MVP]

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)
 
P

Patrice

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).
 
J

Just Me

Must be me then, ill try it again
Cheers


Mark Rae said:
Works perfectly:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top