开发者

How to use the [HandleError] attribute in ASP.NET MVC?

I've got some custom error handling in my ASP.NET MVC site -> seems to be working fine.

I have an Error Controller, like this :-

/Controller/ErrorController.cs

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Unknown()
{
    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    return View("Error");
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult NotFound()
{
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    return View("Error");
}

(i've also removed some other text, like the message to display, etc..)

The view is located like..

/Views/Error/Error.aspx

Now, i'm not sure how I can 'call' the Unknown method (above) when an error occurs. I thought I could use the [HandleError] attribute, but this only allows me to specify a View (and I'm don't think i'm doing that properly, cause the er开发者_运维问答ror url is really weird).


The [HandleError] attribute allows you to specify error pages for unhandled exceptions in your controller classes as opposed to the default custom error handling provided by ASP.NET. I'd rather use traditional ASP.NET custom error handling unless in some controllers you want to ovveride default custom error behaviour.

 <customErrors mode="On" defaultRedirect="~/Error/Unknown">
      <error statusCode="404" redirect="~/Error/PageNotFound"></error>
      <error statusCode="500" redirect="~/Error/Server"></error>
      <error statusCode="503" redirect="~/Error/Server"></error>
    </customErrors>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜