custom httphandler throwing exception while doing rewrite path
I want redirect to a file which is at the root level of the application , but i am getting the following error.
I did the following things to achieve the same
- I have a virtual directory under root which is sample
- Added handler with some custom name like .zpy which resolves to .net root/sample(sample is a virtual directory)
Added the mapping in the web.config
<add verb="*" path="*.zpy" type="MyUrlHttpHandler, WebProj"/>
And this is the code
string finalUrl= "http://www.test.com/test.asp"; context.RewritePath(finalUrl); IHttpHandler hnd = PageParser.GetCompiledPageInstance(finalUrl, null, context); hnd.ProcessRequest(context);
I get the following error
http://www.test.com/test.asp is not a valid virtual path.
The code is getting executed but i am unable to send the request to test.asp which is at root le开发者_如何转开发vel.
i tried moving the handler to root and having handler at the root level , but its still throwing the same error
exception stack trace
HttpException (0x80004005): 'http://locwww.test.com/test.asp' is not a valid virtual path.]
System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options) +8855691
System.Web.HttpContext.RewritePath(String path, Boolean rebaseClientPath) +116
System.Web.HttpContext.RewritePath(String path) +6
ADC.Web.Code.ApcUrlHttpHandler.ProcessRequest(HttpContext context) in C:\Projects\webproj\urlHttpHandler.cs:30
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
What you're doing makes no sense. Is the "http://www.test.com/test.asp" file an asp.net file? If not, then it won't be possible to get a compiled page instance!
Try using Response.Redirect or Server.Transfer instead.
Given your discussion elsewhere, if you'd actually be happy with a redirect, then that's simple:
Response.Redirect("http://www.test.com/test.asp", true);
That's going to be a lot simpler than trying to serve the original content from this request.
if you're trying to redirect to a page/handler in the root folder then simple using this path: ~/mypage.aspx.
And rather than use ReWritePath and everything else you're doing in your handler just use
Response.Redirect("~/test.asp");
This has the advantage is working in your dev environment as well as in production because you're not hard coding the complete url. ASP.NET will resolve the "~/" to be the root of your application even if the application is in a subfolder.
精彩评论