SORU
1 Mayıs 2009, Cuma


Hata ASP.NET MVC İşleme

Nasıl doğru istisnalar ASP.NET MVC denetleyicileri atılan işleyebilir miyim? HandleError öznitelik yalnızca özel durumlar MVC altyapısı tarafından atılan ve istisnalar kendi kodumu tarafından atılan olmayan bir süreç gibi görünüyor.

Bu web kullanarak.config

<customErrors mode="On">
    <error statusCode="401" redirect="/Errors/Http401" />
</customErrors>

aşağıdaki kod ile

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // Force a 401 exception for testing
            throw new HttpException(401, "Unauthorized");
        }
    }
}

ne umduğumu neden yok. Bunun yerine genel ASP.NET hata benim web sayfasını değiştirmek için söylüyorum.config gerçek hata bilgilerini görmek için. Ancak yerine geçersiz bir Görünüm döndüğüm bir özel durum üretiliyor, /Shared/Görüş/Hata alıyorum.sayfa SEO:

return View("DoesNotExist");

Atma istisnalar içinde bir Denetleyicisi gibi yaptım yukarıdaki gibi bypass tüm HandleError işlevselliği, bu yüzden ne doğru yol oluşturmak hata sayfaları ve nasıl oynanıyor güzel ile MVC altyapı?

CEVAP
1 Mayıs 2009, Cuma


Controller.OnException(ExceptionContext context). Geçersiz kılmak.

protected override void OnException(ExceptionContext filterContext)
{
    // Bail if we can't do anything; app will crash.
    if (filterContext == null)
        return;
        // since we're handling this, log to elmah

    var ex = filterContext.Exception ?? new Exception("No further information exists.");
    LogException(ex);

    filterContext.ExceptionHandled = true;
    var data = new ErrorPresentation
        {
            ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
            TheException = ex,
            ShowMessage = !(filterContext.Exception == null),
            ShowLink = false
        };
    filterContext.Result = View("ErrorPage", data);
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • InfoPuppet

    InfoPuppet

    15 Kasım 2011
  • Moodle

    Moodle

    11 Mayıs 2010
  • Need for Speed

    Need for Spe

    8 ŞUBAT 2006