I'm trying to set a custom 404 error page for my web application. The trouble is that this application will be deployed to a number of different environments. Sometimes it will be in a virtual directory and sometimes it won't.
我正在尝试为我的Web应用程序设置自定义404错误页面。问题是该应用程序将部署到许多不同的环境中。有时它会在虚拟目录中,有时则不会。
I have the error page in a directory called ErrorPages and have set up my config like this:
我在名为ErrorPages的目录中有错误页面,并设置了我的配置,如下所示:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
The trouble is when I deploy this to the root of a web site, the /VirtualDir
part needs to be removed. If I remove it before deployment then I need to add it back in when deploying to a virtual directory. Is there any way I can set the path to be relative to the virtual directory and not to the site?
问题是当我将其部署到网站的根目录时,需要删除/ VirtualDir部分。如果我在部署之前删除它,那么我需要在部署到虚拟目录时将其重新添加。有什么办法可以设置相对于虚拟目录而不是网站的路径?
I have tried using a ~
, but that does not work either, like this:
我试过使用〜,但这也不起作用,像这样:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="~/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
2 个解决方案
#1
4
You could use web.config transforms to set the path per environment:
您可以使用web.config转换来设置每个环境的路径:
web.config
web.config中
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
web.Release.config
web.Release.config
<httpErrors>
<error statusCode="404" path="/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
#2
2
I was facing similar problem , so I used server side code to redirect to CustomError page with the dynamically generated URL (with or without Virtual Directory) although ~/ successfully redirect to correct path from here.
我遇到了类似的问题,所以我使用服务器端代码重定向到CustomError页面,其中包含动态生成的URL(有或没有虚拟目录),尽管〜/成功重定向到此处的正确路径。
When an error occurs in the application Application_Error fires and eventually this code block is fired:
当应用程序Application_Error触发时发生错误并最终触发此代码块:
if (App.Configuration.DebugMode == DebugModes.ApplicationErrorMessage)
{
string stockMessage = App.Configuration.ApplicationErrorMessage;
// Handle some stock errors that may require special error pages
HttpException httpException = serverException as HttpException;
if (httpException != null)
{
int HttpCode = httpException.GetHttpCode();
Server.ClearError();
if (HttpCode == 404) // Page Not Found
{
Response.StatusCode = 404;
Response.Redirect("~/ErrorPage.aspx"); // ~ works fine no matter site is in Virtual Directory or Web Site
return;
}
}
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Instead of writing you page path in httpErrors section of web.config you can create an app setting and save path there. In the code behind you can get the path from app setting and redirect as describe above.
您可以创建应用程序设置并在其中保存路径,而不是在web.config的httpErrors部分中编写页面路径。在后面的代码中,您可以从应用程序设置和重定向获取路径,如上所述。
I found another similar link and he explained it better than me so just go though it http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/112276/2/Programmatically-configure-customErrors-redirects/
我找到了另一个类似的链接,他解释得比我好,所以只是去http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/112276/2/Programmatically-configure-customErrors-redirects/
#1
4
You could use web.config transforms to set the path per environment:
您可以使用web.config转换来设置每个环境的路径:
web.config
web.config中
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
web.Release.config
web.Release.config
<httpErrors>
<error statusCode="404" path="/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
#2
2
I was facing similar problem , so I used server side code to redirect to CustomError page with the dynamically generated URL (with or without Virtual Directory) although ~/ successfully redirect to correct path from here.
我遇到了类似的问题,所以我使用服务器端代码重定向到CustomError页面,其中包含动态生成的URL(有或没有虚拟目录),尽管〜/成功重定向到此处的正确路径。
When an error occurs in the application Application_Error fires and eventually this code block is fired:
当应用程序Application_Error触发时发生错误并最终触发此代码块:
if (App.Configuration.DebugMode == DebugModes.ApplicationErrorMessage)
{
string stockMessage = App.Configuration.ApplicationErrorMessage;
// Handle some stock errors that may require special error pages
HttpException httpException = serverException as HttpException;
if (httpException != null)
{
int HttpCode = httpException.GetHttpCode();
Server.ClearError();
if (HttpCode == 404) // Page Not Found
{
Response.StatusCode = 404;
Response.Redirect("~/ErrorPage.aspx"); // ~ works fine no matter site is in Virtual Directory or Web Site
return;
}
}
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Instead of writing you page path in httpErrors section of web.config you can create an app setting and save path there. In the code behind you can get the path from app setting and redirect as describe above.
您可以创建应用程序设置并在其中保存路径,而不是在web.config的httpErrors部分中编写页面路径。在后面的代码中,您可以从应用程序设置和重定向获取路径,如上所述。
I found another similar link and he explained it better than me so just go though it http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/112276/2/Programmatically-configure-customErrors-redirects/
我找到了另一个类似的链接,他解释得比我好,所以只是去http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/112276/2/Programmatically-configure-customErrors-redirects/