kk1076
kk1076

Reputation: 1748

Custom error page content not displayed properly

I specified custom error page in web.config like this

<system.web>
   <customErrors mode="RemoteOnly" defaultRedirect="/Error.html" />
</system.web>

The custom error page and its content is displayed properly. but when the url ends with slash, the error page content is displayed and is not displayed properly.

For example:
www.abc.com/product -> error page content is displayed properly
www.abc.com/product/ ->error page content is not displayed properly

Thanks in advance.

EDIT:

When the url ends with any other special characters other than "/", the url redirects correctly to Error.html, but the url ending with "/" is considered as project folder and so the error page is not displayed properly. Correct me if I am wrong. Any suggestions.

Upvotes: 3

Views: 402

Answers (2)

Mike Guthrie
Mike Guthrie

Reputation: 4059

You are wanting to redirect to your error page when a user attempts directory browsing. This is generally an HTTP error, handled by the server, which is handled apart from your ASP.NET processing.

If you are using IIS7+, then you can take the solutions together from How to disable Directory Browse in Web.Config and Asp.net - Web.Config - Custom Errors

Specific to your case, you would want a block in your web.config similar to:

<system.webServer>
  <directoryBrowse enabled="false" />
  <httpErrors errorMode="Custom">
    <remove statusCode="403"/>
    <error statusCode="403" path="~/Error.aspx" responseMode="Redirect"/>
  </httpErrors>
</system.webServer>

Upvotes: 2

user2232273
user2232273

Reputation: 4964

try this:

<customErrors mode="On" defaultRedirect="Error"></customErrors>

and have in your View -> Shared Folder a View with the name "Error.cshtml"

In the View Error.cshtml add this line at the top:

@model System.Web.Mvc.HandleErrorInfo

...your code.....

hope this helps

Upvotes: 1

Related Questions