Url Rewriting in asp.net but maintaining the origi

2019-02-21 19:57发布

Page aspxHandler = (Page)PageParser.GetCompiledPageInstance(virtualPath, context.Server.MapPath(virtualPath), context);

aspxHandler.PreRenderComplete += AspxPage_PreRenderComplete;
aspxHandler.ProcessRequest(context);

When you call Page.Request.Url after this, you get the Url of the page you rewrote to

...what I'm looking for is to do a rewrite, but for Page.Request.Url to remain as the original url that was passed in. Is that possible?

1条回答
Anthone
2楼-- · 2019-02-21 20:30

I had a similar problem using rewriting rules in the web.config. Not sure if this will solve your problem too, but I found that when the url was rewritten, the originally requested URL was accessible through the "HTTP_X_ORIGINAL_URL" server variable.

VB:

 string pathAndQuery = Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL") ? Request.ServerVariables("HTTP_X_ORIGINAL_URL") : Request.Url.PathAndQuery

c#:

 string pathAndQuery = Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL") ? Request.ServerVariables["HTTP_X_ORIGINAL_URL"] : Request.Url.PathAndQuery;

That should get you the original path and querystring of the request before rewriting, whether or not rewriting has taken place.

查看更多
登录 后发表回答