Response.Redirect偶尔会导致错误,但我无法重现它

时间:2009-08-25 08:09:28

标签: asp.net javascript httpexception

我有一个网站会在一段时间不活动后将用户注销。这可以通过以下代码完成:

 window.location = "./logout.aspx?timeout=true";

但是在过去的几天里我收到了三次例外情况:

System.Web.HttpException

The file '/NIR310/Person/logout.aspx' does not exist.

   at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
   at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

首先,错误似乎是显而易见的,因为logout.aspx不包含在“Person”文件夹中,但是在根级别,但为什么不是每次我从“Person”文件夹注销时都会发生?我一遍又一遍地做同样的程序,但错误几乎从未发生过。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

将您的代码更改为以下内容:

window.location = "/logout.aspx?timeout=true";

删除'。'从URL始终是网站的根目录。

修改
正如评论中所述,Erikric希望转到虚拟文件夹“NIR310”的根目录。

window.location = "/NIR310/logout.aspx?timeout=true";

答案 1 :(得分:1)

通过将完整解析的路径写入页面,您可以确保始终拥有站点根目录的正确路径(假设您的注销页面位于根文件夹中):

window.location = '<%= ResolveUrl("~/logout.aspx?timeout=true")%>';

如果您的注销页面不在根文件夹中,请执行以下操作:

window.location = '<%= ResolveUrl("~/Pathtoyourpage/logout.aspx?timeout=true")%>';

这样,即使您的开发和生产路径不同,重定向仍然有效。

希望这有帮助。