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