1

how to handle 400 bad request like stackoverflow in mvc 3 , iis 6 ?

eg: www.stackoverflow.com/a<

return 404 not found page , instead of a YSOD page

updated: why this does not work ?

  <httpErrors errorMode="Detailed">
     <remove statusCode="404" subStatusCode="-1" />
     <error statusCode="404" subStatusCode="-1" path="/notfound" responseMode="ExecuteURL" />
     <error statusCode="400" subStatusCode="-1" path="/Error" responseMode="ExecuteURL" />
  </httpErrors>
dfang
  • 1,366
  • 15
  • 41
  • Look at this Question :how can i properly handle 404 in asp.net mvc http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc – Wahid Bitar Jun 04 '12 at 10:36
  • and check this too http://stackoverflow.com/questions/4911212/how-to-implement-proper-http-error-handling-in-net-mvc-2 – VJAI Jun 04 '12 at 14:03

1 Answers1

1

Use customErrors tag of web.config:

<customErrors mode="On" defaultRedirect="UrlToRedirect" >
  <error statusCode="400" redirect="UrlToRedirect"/>
</customErrors>

Eg.:

<customErrors mode="On" defaultRedirect="~/Error/Index">
  <error statusCode="400" redirect="~/Error/Index"/>
</customErrors>

if UrlToRedirect = "~/Error/Index", Here, in this url, "Error" is the name of controller & "Index" is the name of Action method which returns Error View Page.

public class ErrorController : Controller
    {
        public ActionResult Index()
        {
            return View("Error");
        }
    }

In the "\Views\Shared Folder" of you application, you have "Error.cshtml" view page.

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52