URL encoding in global.asax

L

Lau Lei Cheong

One of the "seems to be silly" way to do so is to store the URL in session,
so it becomes something like:

HttpContext.Current.Session =
HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl);

Note: I've not verify the above code so there may be typos.

See if others have better way.

Anyway, why do you want such function? You could have a global variable on
the pages that need this and encode it on Page_Load() eventhandler...
 
S

SMG

Please go though this...
*******************************
Hi All,
I have created an application which is working fine and is in about to
launch, now suddenly my mgmt says there are chances that Scrip ID( a
particular id and not prim key) may have special characters like '&,*,),(
or/'

This data(field/key) I am passing this value as a querystring. e.g.

value to be passed : ABC
http://localhost/myProj/abc.aspx?ScripID=ABC
this works fine,

But when I have special characters like [ABC&D] then the value retrieved is
wrong it just retrieves ABC and not complete ID [ABC&D]
value to be passed : ABC&D
http://localhost/myProj/abc.aspx?ScripID=ABC&D

how do I overcome this, I know I can do it like we have %20 for space and
like wise for & there will be something, but this will be a major change to
my application, can I do this at one end some where in web.config or in aspx
page?

Regards,
Shailesh Gajare


One of the "seems to be silly" way to do so is to store the URL in session,
so it becomes something like:

HttpContext.Current.Session =
HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl);

Note: I've not verify the above code so there may be typos.

See if others have better way.

Anyway, why do you want such function? You could have a global variable on
the pages that need this and encode it on Page_Load() eventhandler...
 
L

Lau Lei Cheong

In that way I suggest you to decode the raw url yourself instead of relying
on Request.QueryString(), I believe the QueryString is already decoded at
the time of BeginRequest...

If you are the one who prepare those URLs, of course you can supply the %XX
style character at the string in the beginning (you can always find the
ascii byte values using CharacterMap located in Accessories -> System
Tools.)

If you are passing them in forms, use POST instead of GET will give you
better chance of doing so.

Sorry for not having good suggestion to tell, but your mgmt really shouldn't
allow characters other than alphanumeric and '_', other chacracters are
likely to case problems in one program or the others. (consider SQL strings
in DB applications)

SMG said:
Please go though this...
*******************************
Hi All,
I have created an application which is working fine and is in about to
launch, now suddenly my mgmt says there are chances that Scrip ID( a
particular id and not prim key) may have special characters like '&,*,),(
or/'

This data(field/key) I am passing this value as a querystring. e.g.

value to be passed : ABC
http://localhost/myProj/abc.aspx?ScripID=ABC
this works fine,

But when I have special characters like [ABC&D] then the value retrieved
is
wrong it just retrieves ABC and not complete ID [ABC&D]
value to be passed : ABC&D
http://localhost/myProj/abc.aspx?ScripID=ABC&D

how do I overcome this, I know I can do it like we have %20 for space and
like wise for & there will be something, but this will be a major change
to
my application, can I do this at one end some where in web.config or in
aspx
page?

Regards,
Shailesh Gajare


One of the "seems to be silly" way to do so is to store the URL in
session,
so it becomes something like:

HttpContext.Current.Session =
HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl);

Note: I've not verify the above code so there may be typos.

See if others have better way.

Anyway, why do you want such function? You could have a global variable on
the pages that need this and encode it on Page_Load() eventhandler...

SMG said:
Hi All,
I want to do url encoding, can I do this in global.asax once for all?

Suppose someone request for a url http://localhost/test/test.aspx?ID=M&M

and I want my web server to do understand the url as
http://localhost/test/test.aspx?ID=M&M

Regards,
Shailesh Gajare
 
L

Lau Lei Cheong

That's right.

Just picking small mistake, it should be UrlEncode("ABC&D")... :)
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top