欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net Core中使用ExceptionFilter過濾器的方法

 更新時(shí)間:2020年03月03日 11:32:34   作者:江北、  
這篇文章主要介紹了.Net Core中使用ExceptionFilter過濾器的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

.Net Core中有各種Filter,分別是AuthorizationFilter、ResourceFilter、ExceptionFilter、ActionFilter、ResultFilter??梢园阉麄兛醋魇?Net Core自帶的AOP的擴(kuò)展封裝。

今天來看其中的一種:ExceptionFilter(用于全局的異常處理)

首先新建一個(gè).Net Core MVC的項(xiàng)目

新建一個(gè)控制器:

這里我們可以看到代碼運(yùn)行到16行時(shí)會(huì)報(bào)一個(gè)索引項(xiàng)超出集合范圍的錯(cuò)誤

按照常規(guī)的思維我們在代碼中會(huì)加異常處理,如下:

try
  {
    var range = Enumerable.Range(1, 3).ToArray();
    var result = range[4];

    return View();
  }
  catch (Exception ex)
  {
    throw new Exception(ex.Message);
  }

但是每個(gè)方法都這樣加會(huì)不會(huì)覺得很煩?有沒有想過只寫一次就可以了

上代碼:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ExceptionFilter.Filter
{
  public class CustomerExceptionFilter : Attribute, IExceptionFilter
  {
    private readonly IHostingEnvironment _hostingEnvironment;
    private readonly IModelMetadataProvider _modelMetadataProvider;

    public CustomerExceptionFilter(IHostingEnvironment hostingEnvironment,
      IModelMetadataProvider modelMetadataProvider)
    {
      _hostingEnvironment = hostingEnvironment;
      _modelMetadataProvider = modelMetadataProvider;
    }
    /// <summary>
    /// 發(fā)生異常進(jìn)入
    /// </summary>
    /// <param name="context"></param>
    public async void OnException(ExceptionContext context)
    {
      if (!context.ExceptionHandled)//如果異常沒有處理
      {
        if (_hostingEnvironment.IsDevelopment())//如果是開發(fā)環(huán)境
        {
          var result = new ViewResult { ViewName = "../Handle/Index" };
          result.ViewData = new ViewDataDictionary(_modelMetadataProvider,
                                context.ModelState);
          result.ViewData.Add("Exception", context.Exception);//傳遞數(shù)據(jù)
          context.Result = result;
        }
        else
        {
          context.Result = new JsonResult(new
          {
            Result = false,
            Code = 500,
            Message = context.Exception.Message
          });
        }
        context.ExceptionHandled = true;//異常已處理
      }
    }
  }
}

我們在方法中先以特性來使用,加上這句代碼:

[TypeFilter(typeof(CustomerExceptionFilter))]

之后會(huì)跳到這個(gè)視圖:../Handle/Index ,會(huì)將異常信息傳入到此視圖

視圖頁代碼:

<p>Message:@ViewData["Exception"]</p>

(可以自行封裝。。。)

我們還可以定義成全局的

在Startup類中的ConfigureServices方法中加入這句代碼

services.AddControllersWithViews(option =>
 {
   option.Filters.Add<CustomerExceptionFilter>();
 });
//3.0以下的版本好像應(yīng)該這樣寫:services.AddMvc();

到此這篇關(guān)于.Net Core中使用ExceptionFilter過濾器的方法的文章就介紹到這了,更多相關(guān).Net Core使用ExceptionFilter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論