Is it possible to use a Relative path when setting a custom error page in IIS7?

爷,独闯天下 提交于 2019-12-04 22:19:45
Graham King

You could use web.config transforms to set the path per environment:

web.config

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>

web.Release.config

<httpErrors>
  <error statusCode="404" path="/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
Imran Rizvi

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.

When an error occurs in the application Application_Error fires and eventually this code block is fired:

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.

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!