By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Context.Rewritepath following Querystring access

P: n/a
Some time ago I posted a request for help with a problem I was having
sometime ago whereby a 404 error would result if any access to the
Querystring had been made before the Context.Rewritepath is called.

I have recently received a number of emails from people asking if I ever
found a solution, so I thought I'd post what I have done to work around this
problem. I am not saying this is the right solution but it works, and it
solved a huge headache for me.

private System.Collections.Hashtable LocalQuerystring;

public void Application_BeginRequest(object sender, EventArgs e) {
//cast sender into instance of HttpApplication class
HttpApplication application = (HttpApplication)sender;

// retrieve requested Url
string requestedUrl = application.Request.Url.ToString();

// get all querystring data
int qsPos = requestedUrl.IndexOf("?");

// Again not sure if this is the best way of doing this
if(qsPos > -1) {
qs = requestedUrl.Substring(qsPos+1);
} else {
qs = "";
LocalQuerystring = LoadQueryString(qs);

// You can now access any querystring variable using the following
// LocalQuerystring["version"}.ToString()
// This would return the value of the version querystring parameter,
without having to use
// the Request.Querystring object, the below rewritepath should now work
as planned!
// now do required rewritePath redirect
Context.RewritePath("template.aspx?pagename=" +

// Loads all of the parameters passed in the querystring into Hashtable and
returns this Hashtable
private System.Collections.Hashtable LoadQueryString(string qs) {
System.Collections.Hashtable qsHash = new
string[] qsArr = qs.Split("&".ToCharArray());

foreach(string s in qsArr) {
string[] item = s.Split("=".ToCharArray());
try {
qsHash.Add(item[0].ToString(), item[1].ToString());
} catch(System.Exception ex) {}
return qsHash;

Hope the above helps someone out.

On a similar note, one of the other issues we have experienced with this
methodology is that when the page is served the form action uses the the
rewritten path, not the requested url. This means that viewstate data
cannot be loaded and causes an error. To overcome this we output a
javascript function with every page that overwrites the action attribute of
the form with the correct value, i.e. the originally requested path. This
enable the viewstate to be loaded correctly.

Has anyone else out there found a better solution for the above problem? I
hate having to use Javascript to perform this task as it is far less
reliable than actually having the correct action output with the page. Is
there a way to override the rendering of the webform action attribute?

Any help appreciated.


Matt Howeson
Please take a look at our Inigo Content Management System on our website.
C#, driven wysiwyg content management.
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.