Context.Rewritepath following Querystring access

M

Matt Howeson

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.
(http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=66a19
d98.0212240627.3f123293%40posting.google.com)

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
syntax
// 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=" +
application.Server.UrlEncode(Request.Path));
}

// 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
System.Collections.Hashtable();
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.

Thanks,

Matt Howeson
http://www.3internet.com
Please take a look at our Inigo Content Management System on our website.
C#, Asp.net driven wysiwyg content management.
 
Joined
May 10, 2006
Messages
1
Reaction score
0
Try calling Context.Rewritepath (..) twice in a row - cant say I have a clue why it works but it does for us...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top