Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

I have the error page in a directory called ErrorPages and have set up my config like this:

   <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?

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>
like image 277
Martin Brown Avatar asked Jun 09 '11 16:06

Martin Brown


2 Answers

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>
like image 162
Graham King Avatar answered Dec 02 '22 20:12

Graham King


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/

like image 28
Imran Rizvi Avatar answered Dec 02 '22 18:12

Imran Rizvi