Programmatically Producing rewritten URLs?

2019-06-13 16:38发布

问题:

Using IIS Rewrite over a ASP.NET 4.0 application. Is there some function I could call from within ASP.NET that would transform a string URL to the rewritten URL? I would like to be able to do this in code-behind.

Something like this: string ProduceRewrittenUrl(string publicUrl)

回答1:

Yes this the HttpContext.Current.RewritePath(string)

reference: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath.aspx

a global place to call it is the protected void Application_BeginRequest(Object sender, EventArgs e) on global.asax.



回答2:

If you want to do it in code, take a look at using the Application_BeginRequest() event of Global.asax.