"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[3740],{9409:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>d,contentTitle:()=>l,default:()=>x,frontMatter:()=>c,metadata:()=>o,toc:()=>a});var i=r(5893),s=r(1151),t=r(4996);const c={id:"filter",title:"5.3 \u7b5b\u9009\u5668/\u62e6\u622a\u5668/\u8fc7\u6ee4\u5668/AOP",sidebar_label:"5.3 \u7b5b\u9009\u5668/\u62e6\u622a\u5668/\u8fc7\u6ee4\u5668/AOP",description:"\u9762\u5411\u5207\u9762\u7f16\u7a0b\u53ef\u4ee5\u6700\u4f4e\u6210\u672c\u89e3\u51b3\u5168\u5c40\u95ee\u9898"},l=void 0,o={id:"filter",title:"5.3 \u7b5b\u9009\u5668/\u62e6\u622a\u5668/\u8fc7\u6ee4\u5668/AOP",description:"\u9762\u5411\u5207\u9762\u7f16\u7a0b\u53ef\u4ee5\u6700\u4f4e\u6210\u672c\u89e3\u51b3\u5168\u5c40\u95ee\u9898",source:"@site/docs/filter.mdx",sourceDirName:".",slug:"/filter",permalink:"/docs/filter",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/filter.mdx",tags:[],version:"current",lastUpdatedBy:"zhoujiawu",lastUpdatedAt:1686796906,formattedLastUpdatedAt:"Jun 15, 2023",frontMatter:{id:"filter",title:"5.3 \u7b5b\u9009\u5668/\u62e6\u622a\u5668/\u8fc7\u6ee4\u5668/AOP",sidebar_label:"5.3 \u7b5b\u9009\u5668/\u62e6\u622a\u5668/\u8fc7\u6ee4\u5668/AOP",description:"\u9762\u5411\u5207\u9762\u7f16\u7a0b\u53ef\u4ee5\u6700\u4f4e\u6210\u672c\u89e3\u51b3\u5168\u5c40\u95ee\u9898"},sidebar:"docs",previous:{title:"5.2 HttpContext",permalink:"/docs/httpcontext"},next:{title:"5.4 \u8bf7\u6c42\u5ba1\u8ba1\u65e5\u5fd7 (Audit)",permalink:"/docs/audit"}},d={},a=[{value:"5.3.1 \u5173\u4e8e\u7b5b\u9009\u5668",id:"531-\u5173\u4e8e\u7b5b\u9009\u5668",level:2},{value:"5.3.2 \u5e94\u7528\u573a\u666f",id:"532-\u5e94\u7528\u573a\u666f",level:2},{value:"5.3.2.1 \u7b5b\u9009\u5668\u4f18\u70b9",id:"5321-\u7b5b\u9009\u5668\u4f18\u70b9",level:3},{value:"5.3.3 \u652f\u6301\u62e6\u622a\u5e94\u7528",id:"533-\u652f\u6301\u62e6\u622a\u5e94\u7528",level:2},{value:"5.3.4 \u7b5b\u9009\u5668\u7c7b\u578b",id:"534-\u7b5b\u9009\u5668\u7c7b\u578b",level:2},{value:"5.3.4.1 \u63a5\u53e3\u7c7b\u578b",id:"5341-\u63a5\u53e3\u7c7b\u578b",level:3},{value:"5.3.4.2 \u7279\u6027 Attribute
\u7c7b\u578b",id:"5342-\u7279\u6027-attribute-\u7c7b\u578b",level:3},{value:"5.3.5 \u7b5b\u9009\u5668\u6ce8\u518c",id:"535-\u7b5b\u9009\u5668\u6ce8\u518c",level:2},{value:"5.3.5.1 \u5728 Startup.cs
\u4e2d\u6ce8\u518c",id:"5351-\u5728-startupcs-\u4e2d\u6ce8\u518c",level:3},{value:"5.3.5.2 \u7279\u6027\u65b9\u5f0f\u6ce8\u518c",id:"5352-\u7279\u6027\u65b9\u5f0f\u6ce8\u518c",level:3},{value:"5.3.6 \u6388\u6743\u7b5b\u9009\u5668
",id:"536-\u6388\u6743\u7b5b\u9009\u5668",level:2},{value:"5.3.6.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"5361-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.6.2 \u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09
",id:"5362-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",level:3},{value:"5.3.7 \u8d44\u6e90\u7b5b\u9009\u5668
",id:"537-\u8d44\u6e90\u7b5b\u9009\u5668",level:2},{value:"5.3.7.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"5371-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.7.2 \u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09
",id:"5372-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",level:3},{value:"5.3.8 \u64cd\u4f5c\u7b5b\u9009\u5668
",id:"538-\u64cd\u4f5c\u7b5b\u9009\u5668",level:2},{value:"5.3.8.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"5381-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.8.2 ActionFilterAttribute
\u65b9\u5f0f",id:"5382-actionfilterattribute-\u65b9\u5f0f",level:3},{value:"5.3.8.3 \u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09
",id:"5383-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",level:3},{value:"5.3.9 \u5f02\u5e38\u7b5b\u9009\u5668
",id:"539-\u5f02\u5e38\u7b5b\u9009\u5668",level:2},{value:"5.3.9.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"5391-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.9.2 ExceptionFilterAttribute
\u65b9\u5f0f",id:"5392-exceptionfilterattribute-\u65b9\u5f0f",level:3},{value:"5.3.9.3 \u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09
",id:"5393-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",level:3},{value:"5.3.10 \u7ed3\u679c\u7b5b\u9009\u5668
",id:"5310-\u7ed3\u679c\u7b5b\u9009\u5668",level:2},{value:"5.3.10.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"53101-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.10.2 ResultFilterAttribute
\u65b9\u5f0f",id:"53102-resultfilterattribute-\u65b9\u5f0f",level:3},{value:"5.3.10.3 \u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09
",id:"53103-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",level:3},{value:"5.3.10.4 IAlwaysRunResultFilter
",id:"53104-ialwaysrunresultfilter",level:3},{value:"5.3.11 RazorPages \u7b5b\u9009\u5668
",id:"5311-razorpages-\u7b5b\u9009\u5668",level:2},{value:"5.3.11.1 \u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f
",id:"53111-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",level:3},{value:"5.3.11.2 ResultFilterAttribute
\u65b9\u5f0f",id:"53112-resultfilterattribute-\u65b9\u5f0f",level:3},{value:"5.3.12 \u7b5b\u9009\u5668\u53d6\u6d88\u548c\u77ed\u8def",id:"5312-\u7b5b\u9009\u5668\u53d6\u6d88\u548c\u77ed\u8def",level:2},{value:"5.3.13 \u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f\u63a7\u5236",id:"5313-\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f\u63a7\u5236",level:2},{value:"5.3.13.1 \u4e0d\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",id:"53131-\u4e0d\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",level:3},{value:"5.3.13.2 \u76f8\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",id:"53132-\u76f8\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",level:3},{value:"5.3.14 \u7b5b\u9009\u5668\u4f9d\u8d56\u6ce8\u5165",id:"5314-\u7b5b\u9009\u5668\u4f9d\u8d56\u6ce8\u5165",level:2},{value:"5.3.15 \u4e86\u89e3\u66f4\u591a",id:"5315-\u4e86\u89e3\u66f4\u591a",level:2},{value:"5.3.16 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"5316-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function u(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,s.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h2,{id:"531-\u5173\u4e8e\u7b5b\u9009\u5668",children:"5.3.1 \u5173\u4e8e\u7b5b\u9009\u5668"}),"\n",(0,i.jsxs)(n.p,{children:["\u7b5b\u9009\u5668\u53c8\u540d\u8fc7\u6ee4\u5668\uff0c\u62e6\u622a\u5668\uff0c\u5728 ",(0,i.jsx)(n.code,{children:"ASP.NET Core"})," \u4e2d\uff0c\u53ef\u5728\u8bf7\u6c42\u5904\u7406\u7ba1\u9053\u4e2d\u7279\u5b9a\u9636\u6bb5\u4e4b\u524d\u6216\u4e4b\u540e\u8fd0\u884c\u4ee3\u7801\u3002",(0,i.jsx)(n.strong,{children:"\u7b5b\u9009\u5668\u662f\u975e\u5e38\u7ecf\u5178\u7684\u9762\u5411\u5207\u9762\u7f16\u7a0b\u65b9\u5f0f"}),"\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684 ",(0,i.jsx)(n.code,{children:"AOP"})," \u64cd\u4f5c\u3002"]}),"\n",(0,i.jsxs)(n.p,{children:["\u901a\u4fd7\u70b9\u8bf4\u5c31\u662f\u53ef\u4ee5\u5728\u63a7\u5236\u5668 ",(0,i.jsx)(n.code,{children:"Action"})," \u6267\u884c\u524d\u540e\u8fdb\u884c\u5207\u9762\u64cd\u4f5c\u6216\u8fd4\u56de ",(0,i.jsx)(n.code,{children:"Result"})," \u7ed3\u679c\u524d\u540e\u64cd\u4f5c\u3002"]}),"\n",(0,i.jsx)(n.h2,{id:"532-\u5e94\u7528\u573a\u666f",children:"5.3.2 \u5e94\u7528\u573a\u666f"}),"\n",(0,i.jsxs)(n.p,{children:["\u901a\u8fc7\u81ea\u5b9a\u4e49\u7b5b\u9009\u5668\u53ef\u4ee5\u5b9e\u73b0",(0,i.jsxs)(n.strong,{children:["\u9519\u8bef\u5904\u7406\uff0c\u7f13\u5b58\u5904\u7406\uff0c\u6388\u6743\u5904\u7406\uff0c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5b9e\u73b0\u5de5\u4f5c\u5355\u5143\u4e8b\u52a1\uff08",(0,i.jsx)(n.code,{children:"Uow"}),"\uff09\u7b49\u7b49\u5207\u9762\u64cd\u4f5c"]}),"\uff0c\u4ece\u800c\u4f7f\u4e1a\u52a1\u903b\u8f91\u548c\u7cfb\u7edf\u884c\u4e3a\u903b\u8f91\u8fdb\u884c\u5206\u79bb\u3002"]}),"\n",(0,i.jsx)(n.h3,{id:"5321-\u7b5b\u9009\u5668\u4f18\u70b9",children:"5.3.2.1 \u7b5b\u9009\u5668\u4f18\u70b9"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"\u6613\u62d3\u5c55\uff0c\u6613\u96c6\u6210"}),"\n",(0,i.jsx)(n.li,{children:"\u4e1a\u52a1\u548c\u7cfb\u7edf\u903b\u8f91\u5206\u79bb\uff0c\u4e0d\u5e72\u6270\u4e1a\u52a1\u4ee3\u7801"}),"\n",(0,i.jsx)(n.li,{children:"\u53ef\u5b9e\u73b0\u63a5\u53e3\u591a\u7ef4\u5ea6\u63a7\u5236\uff0c\u5982\u8bf7\u6c42\u53c2\u6570\u7be1\u6539\uff0c\u8fd4\u56de\u503c\u7be1\u6539\uff0c\u9650\u6d41\uff0c\u5206\u5e03\u5f0f\u4e8b\u52a1\u652f\u6301"}),"\n",(0,i.jsx)(n.li,{children:"..."}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"533-\u652f\u6301\u62e6\u622a\u5e94\u7528",children:"5.3.3 \u652f\u6301\u62e6\u622a\u5e94\u7528"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"Mvc/WebAPI"})," \u63a7\u5236\u5668/",(0,i.jsx)(n.code,{children:"Action"})]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"Razor Pages"})," \u9875\u9762"]}),"\n",(0,i.jsxs)(n.li,{children:["\u6846\u67b6\u63d0\u4f9b\u7684 ",(0,i.jsx)(n.code,{children:"\u52a8\u6001 WebAPI"})]}),"\n",(0,i.jsx)(n.li,{children:"\u6240\u6709\u8bf7\u6c42\u8d44\u6e90"}),"\n",(0,i.jsx)(n.li,{children:"\u5168\u5c40\u5f02\u5e38"}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"534-\u7b5b\u9009\u5668\u7c7b\u578b",children:"5.3.4 \u7b5b\u9009\u5668\u7c7b\u578b"}),"\n",(0,i.jsx)("img",{src:(0,t.Z)("img/f1.png")}),"\n",(0,i.jsx)(n.h3,{id:"5341-\u63a5\u53e3\u7c7b\u578b",children:"5.3.4.1 \u63a5\u53e3\u7c7b\u578b"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u6388\u6743\u7b5b\u9009\u5668"}),"\uff1a\u8be5\u7b5b\u9009\u5668\u662f\u6700\u5148\u8fd0\u884c\uff0c\u7528\u4e8e\u786e\u5b9a\u662f\u5426\u5df2\u9488\u5bf9\u8bf7\u6c42\u4e3a\u7528\u6237\u6388\u6743\u3002 \u5982\u679c\u8bf7\u6c42\u672a\u83b7\u6388\u6743\uff0c\u6388\u6743\u7b5b\u9009\u5668\u53ef\u4ee5\u8ba9\u7ba1\u9053\u77ed\u8def\u3002","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAuthorizationFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncAuthorizationFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"AuthorizeFilter"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u8d44\u6e90\u7b5b\u9009\u5668"}),"\uff1a\u6388\u6743\u540e\u8fd0\u884c\uff0c\u5982\u679c\u9700\u8981\u662f\u5927\u90e8\u5206\u8bf7\u6c42\u7ba1\u9053\u77ed\u8def\uff0c\u5b83\u5c06\u4f1a\u5f88\u6709\u7528","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IResourceFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncResourceFilter"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u64cd\u4f5c\u7b5b\u9009\u5668"}),"\uff1a\u5728\u8c03\u7528\u64cd\u4f5c\u65b9\u6cd5\u4e4b\u524d\u548c\u4e4b\u540e\u8fd0\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u6539\u4f20\u9012\u7684\u53c2\u6570\uff0c\u8fd4\u56de\u7ed3\u679c\u7b49\uff0c",(0,i.jsxs)(n.strong,{children:["\u4e0d\u53ef\u5728 ",(0,i.jsx)(n.code,{children:"Razor Pages"})," \u4e2d\u4f7f\u7528"]}),"\u3002","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IActionFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncActionFilter"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u5f02\u5e38\u7b5b\u9009\u5668"}),"\uff1a\u5728\u5411\u54cd\u5e94\u6b63\u6587\u5199\u5165\u4efb\u4f55\u5185\u5bb9\u4e4b\u524d\uff0c\u5bf9\u672a\u7ecf\u5904\u7406\u7684\u5f02\u5e38\u5e94\u7528\u5168\u5c40\u7b56\u7565\u3002","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IExceptionFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncExceptionFilter"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u7ed3\u679c\u7b5b\u9009\u5668"}),"\uff1a\u5728\u6267\u884c\u64cd\u4f5c\u7ed3\u679c\u4e4b\u524d\u548c\u4e4b\u540e\u7acb\u5373\u8fd0\u884c\u4ee3\u7801\uff0c",(0,i.jsx)(n.strong,{children:"\u4ec5\u5f53\u64cd\u4f5c\u65b9\u6cd5\u6210\u529f\u6267\u884c\u65f6\uff0c\u5b83\u4eec\u624d\u4f1a\u8fd0\u884c"}),"\u3002 \u5bf9\u4e8e\u5fc5\u987b\u56f4\u7ed5\u89c6\u56fe\u6216\u683c\u5f0f\u5316\u7a0b\u5e8f\u7684\u6267\u884c\u7684\u903b\u8f91\uff0c\u5b83\u4eec\u5f88\u6709\u7528\u3002","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IResultFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncResultFilter"})}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"IAlwaysRunResultFilter"}),"\uff1a\u8be5\u63a5\u53e3\u4e3b\u8981\u9488\u5bf9\u6240\u6709\u64cd\u4f5c\u7ed3\u679c\u8fd0\u884c\u62e6\u622a\uff0c\u4e5f\u5c31\u662f\u5373\u4f7f ",(0,i.jsx)(n.code,{children:"IResourceFilter"})," \u8bbe\u7f6e\u4e86 ",(0,i.jsx)(n.code,{children:"Result"})," \u4ecd\u4f1a\u6267\u884c\u5e76\u83b7\u53d6\u6700\u7ec8\u7684 ",(0,i.jsx)(n.code,{children:"Result"})]}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncAlwaysRunResultFilter"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"Razor Pages \u7b5b\u9009\u5668"}),"\uff1a\u5141\u8bb8 ",(0,i.jsx)(n.code,{children:"Razor Pages"})," \u5728\u8fd0\u884c ",(0,i.jsx)(n.code,{children:"Razor"})," \u9875\u9762\u5904\u7406\u7a0b\u5e8f\u524d\u540e\u8fd0\u884c\u4ee3\u7801\uff0c\u548c",(0,i.jsx)(n.strong,{children:"\u64cd\u4f5c\u7b5b\u9009\u5668"}),"\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4e0d\u80fd\u5e94\u7528\u5355\u4e2a\u9875\u9762\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002","\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IPageFilter"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"IAsyncPageFilter"})}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.h3,{id:"5342-\u7279\u6027-attribute-\u7c7b\u578b",children:["5.3.4.2 \u7279\u6027 ",(0,i.jsx)(n.code,{children:"Attribute"})," \u7c7b\u578b"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u6388\u6743\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"Attribute"})," + ",(0,i.jsx)(n.code,{children:"IAsyncAuthorizationFilter"}),")\uff1a\u540c\u4e0a\u63a5\u53e3\u7c7b\u578b"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u64cd\u4f5c\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"ActionFilterAttribute"}),")\uff1a\u540c\u4e0a\u63a5\u53e3\u7c7b\u578b"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u5f02\u5e38\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"ExceptionFilterAttribute"}),")\uff1a\u540c\u4e0a\u63a5\u53e3\u7c7b\u578b"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u7ed3\u679c\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"ResultFilterAttribute"}),")\uff1a\u540c\u4e0a\u63a5\u53e3\u7c7b\u578b"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u670d\u52a1\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"ServiceFilterAttribute"}),")\uff1a",(0,i.jsx)(n.strong,{children:"\u652f\u6301\u4f9d\u8d56\u6ce8\u5165"}),"\u7684\u670d\u52a1\u7b5b\u9009\u5668\u7279\u6027"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u7c7b\u578b\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"TypeFilterAttribute"}),")\uff1a",(0,i.jsx)(n.strong,{children:"\u4e0d\u652f\u6301\u4f9d\u8d56\u6ce8\u5165"}),"\u4f46\u53ef\u4ee5\u4f20\u5165\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u53c2\u6570"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.strong,{children:"\u7ec4\u5408\u7279\u6027\u7b5b\u9009\u5668"})," (",(0,i.jsx)(n.code,{children:"Attribute"})," + \u63a5\u53e3\u7c7b\u578b\u65b9\u5f0f)\uff1a\u53ef\u4ee5\u901a\u8fc7\u6d3e\u751f\u81ea ",(0,i.jsx)(n.code,{children:"Attribute"})," \u548c \u7279\u5b9a\u63a5\u53e3\u5b9e\u73b0\uff0c\u5982 ",(0,i.jsx)(n.code,{children:"Attribute, IActionFilter"})]}),"\n"]}),"\n",(0,i.jsxs)(n.admonition,{title:"\u7b5b\u9009\u5668\u9009\u7528\u6280\u5de7",type:"tip",children:[(0,i.jsxs)(n.p,{children:["\u5173\u4e8e\u9009\u62e9\u54ea\u79cd\u7c7b\u578b\u7684\u7b5b\u9009\u5668\u6709\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c",(0,i.jsx)(n.strong,{children:"\u5f53\u4f60\u4e0d\u9700\u8981\u5168\u5c40\u7b5b\u9009\u5668\u7684\u65f6\u5019\u4f7f\u7528\u7279\u6027\u7b5b\u9009\u5668\uff0c\u5426\u5219\u4f7f\u7528\u63a5\u53e3\u7c7b\u578b\u7b5b\u9009\u5668"}),"\u3002"]}),(0,i.jsx)(n.p,{children:(0,i.jsxs)(n.strong,{children:["\u53e6\u5916\u5c3d\u53ef\u80fd\u7684\u4f7f\u7528\u5e26 ",(0,i.jsx)(n.code,{children:"IAsync"})," \u5f00\u5934\u7684\u5f02\u6b65\u7b5b\u9009\u5668\uff0c\u8fd9\u6837\u65e0\u9700\u5206\u5f00\u591a\u4e2a\u65b9\u6cd5\uff0c\u53ef\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u64cd\u4f5c\uff0c\u8fd8\u80fd\u63d0\u9ad8\u541e\u5410\u91cf\u3002"]})})]}),"\n",(0,i.jsxs)(n.admonition,{title:"\u540c\u6b65\u5f02\u6b65\u7b5b\u9009\u5668",type:"important",children:[(0,i.jsx)(n.p,{children:(0,i.jsx)(n.strong,{children:"\u7b5b\u9009\u5668\u63a5\u53e3\u7684\u540c\u6b65\u548c\u5f02\u6b65\u7248\u672c\u4efb\u610f\u5b9e\u73b0\u4e00\u4e2a\uff0c\u800c\u4e0d\u662f\u540c\u65f6\u5b9e\u73b0\u3002"})}),(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"\u8fd0\u884c\u65f6\u4f1a\u5148\u67e5\u770b\u7b5b\u9009\u5668\u662f\u5426\u5b9e\u73b0\u4e86\u5f02\u6b65\u63a5\u53e3"}),"\uff0c\u5982\u679c\u662f\uff0c\u5219\u8c03\u7528\u8be5\u63a5\u53e3\u3002 \u5982\u679c\u4e0d\u662f\uff0c\u5219\u8c03\u7528\u540c\u6b65\u63a5\u53e3\u7684\u65b9\u6cd5\u3002"]}),(0,i.jsxs)(n.p,{children:["\u5982\u679c\u5728\u4e00\u4e2a\u7c7b\u4e2d\u540c\u65f6\u5b9e\u73b0\u5f02\u6b65\u548c\u540c\u6b65\u63a5\u53e3\uff0c\u5219\u4ec5\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5\u3002 \u4f7f\u7528\u62bd\u8c61\u7c7b\uff08\u5982 ",(0,i.jsx)(n.code,{children:"ActionFilterAttribute"}),"\uff09\u65f6\uff0c\u5c06\u4e3a\u6bcf\u79cd\u7b5b\u9009\u5668\u7c7b\u578b\u4ec5\u91cd\u5199\u540c\u6b65\u65b9\u6cd5\u6216\u4ec5\u91cd\u5199\u5f02\u6b65\u65b9\u6cd5\u3002"]})]}),"\n",(0,i.jsx)(n.h2,{id:"535-\u7b5b\u9009\u5668\u6ce8\u518c",children:"5.3.5 \u7b5b\u9009\u5668\u6ce8\u518c"}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"ASP.NET Core"})," \u63d0\u4f9b\u4e86\u591a\u79cd\u7b5b\u9009\u5668\u6ce8\u518c\u65b9\u5f0f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u4e0d\u540c\u7684\u6ce8\u518c\u65b9\u5f0f\u6267\u884c\u987a\u5e8f\u4e0d\u540c\uff0c\u670d\u52a1\u7c7b\u578b\u6ce8\u518c\u6700\u5148\u6267\u884c\uff0c\u5176\u6b21\u662f ",(0,i.jsx)(n.code,{children:"Mvc Filter"})," \u65b9\u5f0f\uff0c\u6700\u540e\u662f\u7279\u6027\u65b9\u5f0f\u3002\u76f8\u540c\u7684\u65b9\u5f0f\u4e2d\u53c8\u6309\u7167\u6ce8\u518c\u524d\u540e\u6765\u51b3\u5b9a\u6267\u884c\u987a\u5e8f\uff0c\u5148\u6ce8\u518c\u5148\u6267\u884c\u3002"]}),"\n",(0,i.jsxs)(n.p,{children:["\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"IOrderedFilter"})," \u63a5\u53e3\u91cd\u5199\u6267\u884c\u987a\u5e8f\uff0c\u5176 ",(0,i.jsx)(n.code,{children:"Order"})," \u5c5e\u6027\u503c\u8d8a\u9ad8\u7684\u5148\u6267\u884c\u3002"]}),"\n",(0,i.jsxs)(n.h3,{id:"5351-\u5728-startupcs-\u4e2d\u6ce8\u518c",children:["5.3.5.1 \u5728 ",(0,i.jsx)(n.code,{children:"Startup.cs"})," \u4e2d\u6ce8\u518c"]}),"\n",(0,i.jsxs)(n.p,{children:["\u6700\u5e38\u89c1\u7684\u6ce8\u518c\u7b5b\u9009\u5668\u7684\u65b9\u5f0f\u5c31\u662f\u5728 ",(0,i.jsx)(n.code,{children:"Startup.cs"})," \u4e2d\u6ce8\u518c\uff0c",(0,i.jsxs)(n.strong,{children:["\u8fd9\u79cd\u65b9\u5f0f\u8868\u793a\u5168\u5c40\u6ce8\u518c\uff0c\u5e94\u7528\u6240\u6709\u63a7\u5236\u5668/",(0,i.jsx)(n.code,{children:"Action"})]})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers {1,3-9,11-15,17-18} title="Startup.cs"',children:'public void ConfigureServices(IServiceCollection services)\r\n{\r\n // Mvc \u65b9\u5f0f\u6ce8\u518c\u4e00\uff0c\u5168\u5c40\u6267\u884c\r\n services.AddControllersWithViews(options =>\r\n {\r\n options.Filters.Add(new AddHeaderAttribute("GlobalAddHeader", "Result filter added to MvcOptions.Filters")); // \u624b\u52a8\u521b\u5efa\u5b9e\u4f8b\uff0c\u5e26\u6784\u9020\u53c2\u6570\r\n options.Filters.Add(typeof(MySampleActionFilter)); // \u7c7b\u578b Type \u65b9\u5f0f\r\n options.Filters.Add(new SampleGlobalActionFilter()); // \u624b\u52a8\u521b\u5efa\u5b9e\u4f8b\uff0c\u65e0\u6784\u9020\u53c2\u6570\r\n });\r\n\r\n // Mvc \u65b9\u5f0f\u6ce8\u518c\u4e8c\uff0c\u5168\u5c40\u6267\u884c\r\n services.Configure(options =>\r\n {\r\n options.Filters.Add();\r\n });\r\n\r\n // Mvc \u6ce8\u518c\u65b9\u5f0f\u4e09\uff0c\u5168\u5c40\u6267\u884c\uff0cFurion \u6846\u67b6\u63d0\u4f9b\u65b9\u5f0f\r\n services.AddMvcFilter();\r\n}\n'})}),"\n",(0,i.jsx)(n.h3,{id:"5352-\u7279\u6027\u65b9\u5f0f\u6ce8\u518c",children:"5.3.5.2 \u7279\u6027\u65b9\u5f0f\u6ce8\u518c"}),"\n",(0,i.jsxs)(n.p,{children:["\u5982\u679c\u7b5b\u9009\u5668\u6d3e\u751f\u81ea ",(0,i.jsx)(n.code,{children:"\u7279\u6027"}),"\uff0c\u5219\u53ef\u901a\u8fc7\u7279\u6027\u65b9\u5f0f\u6ce8\u518c\uff0c",(0,i.jsxs)(n.strong,{children:["\u8fd9\u79cd\u65b9\u5f0f\u8868\u793a\u5c40\u90e8\u6ce8\u518c\uff0c\u53ea\u4f5c\u7528\u4e8e\u7279\u5b9a\u7684\u63a7\u5236\u5668/",(0,i.jsx)(n.code,{children:"Action"})]})]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"\u76f4\u63a5\u8d34\u65b9\u5f0f"}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,7-9}",children:"// \u5b9a\u4e49\u7ed3\u679c\u7279\u6027\u7b5b\u9009\u5668\r\npublic class AddHeaderAttribute : ResultFilterAttribute\r\n{\r\n // ...\r\n}\r\n\r\n// \u76f4\u63a5\u8d34\u65b9\u5f0f\uff0c\u5bf9\u4e8e\u52a8\u6001 WebAPI \u4e5f\u662f\u4e00\u6837\u7684\r\n[AddHeader]\r\npublic class SampleController : Controller\r\n{\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\u901a\u8fc7 ",(0,i.jsx)(n.code,{children:"[ServiceFilter]"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u7684\u7279\u6027\u7b5b\u9009\u5668\u5305\u542b\u6784\u9020\u51fd\u6570\u6ce8\u5165\u670d\u52a1\u5e94\u7528\u573a\u666f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5fc5\u987b\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u901a\u8fc7 ",(0,i.jsx)(n.code,{children:"services.AddScoped"})," \u6ce8\u518c\u3002"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3-5}",children:"public class MyActionFilterAttribute : ActionFilterAttribute\r\n{\r\n // \u6ce8\u5165\u670d\u52a1\r\n private readonly PositionOptions _settings;\r\n public MyActionFilterAttribute(IOptions options)\r\n {\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsxs)(n.strong,{children:["\u9700\u5148\u5728 ",(0,i.jsx)(n.code,{children:"Startup.cs"})," \u4e2d\u6ce8\u518c\u7b5b\u9009\u5668"]})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Startup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {3-5}",children:"public class SampleController : Controller\r\n{\r\n // \u901a\u8fc7 [ServiceFilter] \u65b9\u5f0f\r\n [ServiceFilter(typeof(MyActionFilterAttribute))]\r\n public IActionResult Index2()\r\n {\r\n // ...\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\u901a\u8fc7 ",(0,i.jsx)(n.code,{children:"[TypeFilter]"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"})," \u548c ",(0,i.jsx)(n.code,{children:"[ServiceFilter]"})," \u7c7b\u4f3c\uff0c\u552f\u4e00\u7684\u533a\u522b\u5c31\u662f ",(0,i.jsx)(n.code,{children:"[TypeFilter]"})," ",(0,i.jsx)(n.strong,{children:"\u4e0d\u652f\u6301\u6784\u9020\u51fd\u6570\u6ce8\u5165\u670d\u52a1\uff0c\u4f46\u53ef\u4ee5\u4f20\u9012\u57fa\u5143\u7c7b\u578b\u6784\u9020\u51fd\u6570\u53c2\u6570"}),"\u3002"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3-4,11-13}",children:'public class MyLogFilterAttribute : ActionFilterAttribute\r\n{\r\n // \u6784\u9020\u51fd\u6570\u5305\u542b\u57fa\u5143\u7c7b\u578b\u53c2\u6570\r\n public MyLogFilterAttribute(string message, int level)\r\n {\r\n }\r\n}\r\n\r\npublic class SampleController : Controller\r\n{\r\n // \u901a\u8fc7 [TypeFilter] \u65b9\u5f0f\r\n [TypeFilter(typeof(MyLogFilterAttribute), Arguments = new object[] { "Message", 10 })]\r\n public IActionResult Index2()\r\n {\r\n // ...\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.h2,{id:"536-\u6388\u6743\u7b5b\u9009\u5668",children:["5.3.6 ",(0,i.jsx)(n.code,{children:"\u6388\u6743\u7b5b\u9009\u5668"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u901a\u8fc7\u6388\u6743\u7b5b\u9009\u5668\u53ef\u4ee5\u5b9e\u73b0\u5728\u6240\u6709\u8bf7\u6c42\u5230\u8fbe\u63a7\u5236\u5668/",(0,i.jsx)(n.code,{children:"Action"})," \u4e4b\u524d\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5982\u679c\u6388\u6743\u5931\u8d25\uff0c\u76f4\u63a5\u8df3\u8f6c\u5230\u767b\u5f55\u6216\u8005\u8fd4\u56de ",(0,i.jsx)(n.code,{children:"401"}),"\u3002"]}),"\n",(0,i.jsxs)(n.h3,{id:"5361-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.6.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {9-12,32-50}",children:'using Microsoft.AspNetCore.Authorization;\r\nusing Microsoft.AspNetCore.Mvc;\r\nusing Microsoft.AspNetCore.Mvc.Authorization;\r\nusing Microsoft.AspNetCore.Mvc.Controllers;\r\nusing Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\n/// \r\n/// \u81ea\u5b9a\u4e49\u6388\u6743\u7b5b\u9009\u5668\r\n/// \r\npublic class MyAuthorizationFilter : IAsyncAuthorizationFilter\r\n{\r\n public async Task OnAuthorizationAsync(AuthorizationFilterContext context)\r\n {\r\n Console.WriteLine("\u6388\u6743\u68c0\u67e5......");\r\n\r\n // \u83b7\u53d6\u63a7\u5236\u5668\u4fe1\u606f\r\n var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;\r\n\r\n // \u83b7\u53d6\u63a7\u5236\u5668\u7c7b\u578b\r\n var controllerType = actionDescriptor!.ControllerTypeInfo;\r\n\r\n // \u83b7\u53d6 Action \u7c7b\u578b\r\n var methodType = actionDescriptor.MethodInfo;\r\n\r\n // \u662f\u5426\u533f\u540d\u8bbf\u95ee\r\n var allowAnonymouse = context.Filters.Any(u => u is IAllowAnonymousFilter)\r\n || controllerType.IsDefined(typeof(AllowAnonymousAttribute), true)\r\n || methodType.IsDefined(typeof(AllowAnonymousAttribute), true);\r\n\r\n // \u4e0d\u662f\u533f\u540d\u624d\u5904\u7406\u6743\u9650\u68c0\u67e5\r\n if (!allowAnonymouse)\r\n {\r\n Console.Write("\u903b\u8f91\u68c0\u67e5~~~~");\r\n\r\n // \u5728 MVC \u9879\u76ee\u4e2d\uff0c\u5982\u679c\u68c0\u67e5\u5931\u8d25\uff0c\u5219\u8df3\u8f6c\u5230\u767b\u5f55\u9875\r\n if (typeof(Controller).IsAssignableFrom(controllerType.AsType()))\r\n {\r\n context.Result = new RedirectResult("~/Login");\r\n }\r\n // WebAPI \u6216\u8005\u5176\u4ed6\u7c7b\u578b\r\n else\r\n {\r\n // \u8fd4\u56de\u672a\u6388\u6743\r\n context.Result = new UnauthorizedResult();\r\n }\r\n }\r\n // \u5426\u5219\u76f4\u63a5\u8df3\u8fc7\u5904\u7406\r\n else await Task.CompletedTask;\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[TypeFilter(typeof(MyAuthorizationFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"[ServiceFilter]"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Starup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[ServiceFilter(typeof(MyAuthorizationFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5362-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",children:["5.3.6.2 ",(0,i.jsx)(n.code,{children:"\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ea\u9700\u8981\u4e0a\u8ff0\u63a5\u53e3\u65b9\u5f0f\u4e2d\u6dfb\u52a0\u6d3e\u751f ",(0,i.jsx)(n.code,{children:"Attribute"})," \u5e76\u8bbe\u7f6e ",(0,i.jsx)(n.code,{children:"[AttributeUsage]"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,4}",children:"[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]\r\npublic class MyAuthorizationFilterAttribute : Attribute, IAsyncAuthorizationFilter\r\n{\r\n public async Task OnAuthorizationAsync(AuthorizationFilterContext context)\r\n {\r\n // \u4ee3\u7801\u540c\u4e0a\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyAuthorizationFilter]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h2,{id:"537-\u8d44\u6e90\u7b5b\u9009\u5668",children:["5.3.7 ",(0,i.jsx)(n.code,{children:"\u8d44\u6e90\u7b5b\u9009\u5668"})]}),"\n",(0,i.jsx)(n.p,{children:"\u8d44\u6e90\u7b5b\u9009\u5668\u4f7f\u7528\u9891\u7387\u8f83\u5c11\uff0c\u901a\u5e38\u7528\u6765\u5904\u7406\u8d44\u6e90\u7f13\u5b58\u6216\u8005\u963b\u6b62\u6a21\u578b\uff08\u503c\uff09\u7ed1\u5b9a\u64cd\u4f5c\u3002"}),"\n",(0,i.jsxs)(n.h3,{id:"5371-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.7.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {6,8,11-26}",children:"using Microsoft.AspNetCore.Mvc.Filters;\r\nusing Microsoft.AspNetCore.Mvc.ModelBinding;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyResourceFilter : IAsyncResourceFilter\r\n{\r\n public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)\r\n {\r\n // \u83b7\u53d6\u6240\u6709\u8d44\u6e90\u63d0\u4f9b\u5668\u5de5\u5382\r\n var valueProviderFactories = context.ValueProviderFactories;\r\n\r\n // \u6bd4\u5982\u8fd9\u91cc\u5224\u65ad\u5982\u679c\u662f Form \u8868\u5355\u65b9\u5f0f\u63d0\u4ea4\u5c31\u5c31\u4e0d\u7ed9\u53c2\u6570\u590d\u5236\r\n var formValueProviderFactory = valueProviderFactories\r\n .OfType()\r\n .FirstOrDefault();\r\n if (formValueProviderFactory != null)\r\n {\r\n // \u79fb\u9664 Form \u8868\u5355\u7ed1\u5b9a\u503c\u63d0\u4f9b\u5668\u5668\r\n context.ValueProviderFactories.Remove(formValueProviderFactory);\r\n }\r\n\r\n // .... \u66f4\u591a\u64cd\u4f5c\r\n\r\n // \u8d44\u6e90\u8bf7\u6c42\u6210\u529f\u540e\r\n var resourceContext = await next();\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[TypeFilter(typeof(MyResourceFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"[ServiceFilter]"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Starup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[ServiceFilter(typeof(MyResourceFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5372-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",children:["5.3.7.2 ",(0,i.jsx)(n.code,{children:"\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ea\u9700\u8981\u4e0a\u8ff0\u63a5\u53e3\u65b9\u5f0f\u4e2d\u6dfb\u52a0\u6d3e\u751f ",(0,i.jsx)(n.code,{children:"Attribute"})," \u5e76\u8bbe\u7f6e ",(0,i.jsx)(n.code,{children:"[AttributeUsage]"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,4}",children:"[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]\r\npublic class MyResourceFilterAttribute : Attribute, IAsyncResourceFilter\r\n{\r\n public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u540c\u4e0a\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyResourceFilter]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h2,{id:"538-\u64cd\u4f5c\u7b5b\u9009\u5668",children:["5.3.8 ",(0,i.jsx)(n.code,{children:"\u64cd\u4f5c\u7b5b\u9009\u5668"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u64cd\u4f5c\u7b5b\u9009\u5668\u662f\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684\u7b5b\u9009\u5668\uff0c\u901a\u5e38\u7528\u6765\u63a7\u5236\u8fdb\u5165 ",(0,i.jsx)(n.code,{children:"Action"})," \u4e4b\u524d\uff08\u6b64\u65f6\u6a21\u578b\u7ed1\u5b9a\u5df2\u7ecf\u5b8c\u6210\uff09\u548c ",(0,i.jsx)(n.code,{children:"Action"})," \u6267\u884c\u4e4b\u540e\uff08\u6b64\u65f6 ",(0,i.jsx)(n.code,{children:"Result"})," \u8fd8\u672a\u8fd4\u56de\uff09\u3002"]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7b5b\u9009\u5668\u5b9e\u73b0\u5404\u79cd\u9a9a\u64cd\u4f5c\uff0c\u5982",(0,i.jsx)(n.strong,{children:"\u7be1\u6539\u53c2\u6570\uff0c\u7be1\u6539\u8fd4\u56de\u503c\uff0c\u7edf\u4e00\u53c2\u6570\u9a8c\u8bc1\uff0c\u5ba1\u8ba1\u65e5\u5fd7\uff0c\u5b9e\u73b0\u6570\u636e\u5e93\u4e8b\u52a1\u81ea\u52a8\u5f00\u542f\u5173\u95ed"}),"\u7b49\u7b49\u3002"]}),"\n",(0,i.jsxs)(n.h3,{id:"5381-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.8.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {6,8,10,12-53}",children:'using Microsoft.AspNetCore.Mvc.Controllers;\r\nusing Microsoft.AspNetCore.Mvc.Filters;\r\nusing System.Diagnostics;\r\nusing System.Security.Claims;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyActionFilter : IAsyncActionFilter\r\n{\r\n public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)\r\n {\r\n //============== \u8fd9\u91cc\u662f\u6267\u884c\u65b9\u6cd5\u4e4b\u524d\u83b7\u53d6\u6570\u636e ====================\r\n // \u83b7\u53d6\u63a7\u5236\u5668\u3001\u8def\u7531\u4fe1\u606f\r\n var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;\r\n\r\n // \u83b7\u53d6\u8bf7\u6c42\u7684\u65b9\u6cd5\r\n var method = actionDescriptor!.MethodInfo;\r\n\r\n // \u83b7\u53d6 HttpContext \u548c HttpRequest \u5bf9\u8c61\r\n var httpContext = context.HttpContext;\r\n var httpRequest = httpContext.Request;\r\n\r\n // \u83b7\u53d6\u5ba2\u6237\u7aef Ipv4 \u5730\u5740\r\n var remoteIPv4 = httpContext.GetRemoteIpAddressToIPv4();\r\n\r\n // \u83b7\u53d6\u8bf7\u6c42\u7684 Url \u5730\u5740\r\n var requestUrl = httpRequest.GetRequestUrlAddress();\r\n\r\n // \u83b7\u53d6\u6765\u6e90 Url \u5730\u5740\r\n var refererUrl = httpRequest.GetRefererUrlAddress();\r\n\r\n // \u83b7\u53d6\u8bf7\u6c42\u53c2\u6570\uff08\u5199\u5165\u65e5\u5fd7\uff0c\u9700\u5e8f\u5217\u5316\u6210\u5b57\u7b26\u4e32\u540e\u5b58\u50a8\uff09\uff0c\u53ef\u4ee5\u81ea\u7531\u7be1\u6539\uff01\uff01\uff01\uff01\uff01\uff01\r\n var parameters = context.ActionArguments;\r\n\r\n // \u83b7\u53d6\u64cd\u4f5c\u4eba\uff08\u5fc5\u987b\u6388\u6743\u8bbf\u95ee\u624d\u6709\u503c\uff09"userId" \u4e3a\u4f60\u5b58\u50a8\u7684 claims type\uff0cjwt \u6388\u6743\u5bf9\u5e94\u7684\u662f payload \u4e2d\u5b58\u50a8\u7684\u952e\u540d\r\n var userId = httpContext.User?.FindFirstValue("userId");\r\n\r\n // \u8bf7\u6c42\u65f6\u95f4\r\n var requestedTime = DateTimeOffset.Now;\r\n\r\n //============== \u8fd9\u91cc\u662f\u6267\u884c\u65b9\u6cd5\u4e4b\u540e\u83b7\u53d6\u6570\u636e ====================\r\n var actionContext = await next();\r\n\r\n // \u83b7\u53d6\u8fd4\u56de\u7684\u7ed3\u679c\r\n var returnResult = actionContext.Result;\r\n\r\n // \u5224\u65ad\u662f\u5426\u8bf7\u6c42\u6210\u529f\uff0c\u6ca1\u6709\u5f02\u5e38\u5c31\u662f\u8bf7\u6c42\u6210\u529f\r\n var isRequestSucceed = actionContext.Exception == null;\r\n\r\n // \u83b7\u53d6\u8c03\u7528\u5806\u6808\u4fe1\u606f\uff0c\u63d0\u4f9b\u66f4\u52a0\u7b80\u5355\u660e\u4e86\u7684\u8c03\u7528\u548c\u5f02\u5e38\u5806\u6808\r\n var stackTrace = EnhancedStackTrace.Current();\r\n\r\n // \u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u5199\u5165\u65e5\u5fd7\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[TypeFilter(typeof(MyActionFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"[ServiceFilter]"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Starup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[ServiceFilter(typeof(MyActionFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5382-actionfilterattribute-\u65b9\u5f0f",children:["5.3.8.2 ",(0,i.jsx)(n.code,{children:"ActionFilterAttribute"})," \u65b9\u5f0f"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {5,13,26}",children:'using Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyActionAttribute : ActionFilterAttribute\r\n{\r\n /// \r\n /// \u6267\u884c\u64cd\u4f5c\u524d\u540e\r\n /// \r\n /// \r\n /// \r\n /// \r\n public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u53c2\u8003\u63a5\u53e3\u65b9\u5f0f\r\n\r\n return base.OnActionExecutionAsync(context, next);\r\n }\r\n\r\n /// \r\n /// \u8fd4\u56de\u7ed3\u679c\u524d\u540e\r\n /// \r\n /// \r\n /// \r\n /// \r\n public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u53c2\u8003\u63a5\u53e3\u65b9\u5f0f\r\n\r\n return base.OnResultExecutionAsync(context, next);\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyAction]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5383-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",children:["5.3.8.3 ",(0,i.jsx)(n.code,{children:"\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ea\u9700\u8981\u4e0a\u8ff0\u63a5\u53e3\u65b9\u5f0f\u4e2d\u6dfb\u52a0\u6d3e\u751f ",(0,i.jsx)(n.code,{children:"Attribute"})," \u5e76\u8bbe\u7f6e ",(0,i.jsx)(n.code,{children:"[AttributeUsage]"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,4}",children:"[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]\r\npublic class MyActionFilterAttribute : Attribute, IAsyncActionFilter\r\n{\r\n public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u540c\u4e0a\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyActionFilter]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h2,{id:"539-\u5f02\u5e38\u7b5b\u9009\u5668",children:["5.3.9 ",(0,i.jsx)(n.code,{children:"\u5f02\u5e38\u7b5b\u9009\u5668"})]}),"\n",(0,i.jsx)(n.p,{children:"\u5f02\u5e38\u7b5b\u9009\u5668\u4f7f\u7528\u9891\u7387\u4ec5\u6b21\u4e8e\u64cd\u4f5c\u7b5b\u9009\u5668\uff0c\u66f4\u591a\u7528\u4e8e\u7a0b\u5e8f\u51fa\u73b0\u5f02\u5e38\u65f6\u8bb0\u5f55\u65e5\u5fd7\u6216\u8005\u8fd4\u56de\u7edf\u4e00\u7684\u9875\u9762\u64cd\u4f5c\u3002"}),"\n",(0,i.jsxs)(n.h3,{id:"5391-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.9.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {7,9,11-33}",children:"using Microsoft.AspNetCore.Mvc.Controllers;\r\nusing Microsoft.AspNetCore.Mvc.Filters;\r\nusing System.Diagnostics;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyExceptionFilter : IAsyncExceptionFilter\r\n{\r\n public async Task OnExceptionAsync(ExceptionContext context)\r\n {\r\n // \u5982\u679c\u5f02\u5e38\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u6807\u8bb0\u4e86\u5904\u7406\uff0c\u90a3\u4e48\u8fd9\u91cc\u4e0d\u518d\u5904\u7406\r\n if (context.ExceptionHandled) return;\r\n\r\n // \u83b7\u53d6\u63a7\u5236\u5668\u4fe1\u606f\r\n ControllerActionDescriptor? actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;\r\n\r\n // \u83b7\u53d6\u8bf7\u6c42\u7684\u65b9\u6cd5\r\n var method = actionDescriptor!.MethodInfo;\r\n\r\n // \u83b7\u53d6\u5f02\u5e38\u5bf9\u8c61\r\n var exception = context.Exception;\r\n\r\n // \u83b7\u53d6\u8c03\u7528\u5806\u6808\u4fe1\u606f\uff0c\u63d0\u4f9b\u66f4\u52a0\u7b80\u5355\u660e\u4e86\u7684\u8c03\u7528\u548c\u5f02\u5e38\u5806\u6808\r\n var stackTrace = EnhancedStackTrace.Current();\r\n\r\n // \u5176\u4ed6\u5904\u7406~~~\r\n // 1. MVC \u76f4\u63a5\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u9875\u9762\uff0c\u6216\u8005 BadPageResult \u7c7b\u578b\uff0c\u5982\uff1acontext.Result = new BadPageResult(StatusCodes.Status500InternalServerError) { }\r\n\r\n // 2. WebAPI \u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de context.Result = new JsonResult(.....);\r\n\r\n // 3. \u8bb0\u5f55\u65e5\u5fd7\u3002\u3002\u3002\u3002\r\n\r\n await Task.CompletedTask;\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[TypeFilter(typeof(MyExceptionFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"[ServiceFilter]"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Starup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[ServiceFilter(typeof(MyExceptionFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5392-exceptionfilterattribute-\u65b9\u5f0f",children:["5.3.9.2 ",(0,i.jsx)(n.code,{children:"ExceptionFilterAttribute"})," \u65b9\u5f0f"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {5,12}",children:'using Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyExceptionAttribute : ExceptionFilterAttribute\r\n{\r\n /// \r\n /// \u5f02\u5e38\u62e6\u622a\u5668\r\n /// \r\n /// \r\n /// \r\n public override Task OnExceptionAsync(ExceptionContext context)\r\n {\r\n // \u4ee3\u7801\u53c2\u8003\u63a5\u53e3\u65b9\u5f0f\r\n return base.OnExceptionAsync(context);\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyException]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"5393-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",children:["5.3.9.3 ",(0,i.jsx)(n.code,{children:"\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ea\u9700\u8981\u4e0a\u8ff0\u63a5\u53e3\u65b9\u5f0f\u4e2d\u6dfb\u52a0\u6d3e\u751f ",(0,i.jsx)(n.code,{children:"Attribute"})," \u5e76\u8bbe\u7f6e ",(0,i.jsx)(n.code,{children:"[AttributeUsage]"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,4}",children:"[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]\r\npublic class MyExceptionFilterAttribute : Attribute, IAsyncExceptionFilter\r\n{\r\n public async Task OnExceptionAsync(ExceptionContext context)\r\n {\r\n // \u4ee3\u7801\u540c\u4e0a\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyExceptionFilter]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h2,{id:"5310-\u7ed3\u679c\u7b5b\u9009\u5668",children:["5.3.10 ",(0,i.jsx)(n.code,{children:"\u7ed3\u679c\u7b5b\u9009\u5668"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u7ed3\u679c\u63a7\u5236\u5668\u5e38\u7528\u4e8e\u5bf9\u8fd4\u56de\u7684\u7ed3\u679c\u9644\u52a0\u66f4\u591a\u6570\u636e\uff0c\u6bd4\u5982 ",(0,i.jsx)(n.code,{children:"Mvc"})," \u4e2d\u7684 ",(0,i.jsx)(n.code,{children:"ViewBag"}),"\uff0c",(0,i.jsx)(n.code,{children:"ViewData"}),"\uff0c\u6362\u53e5\u8bdd\u8bf4\u4e3b\u8981\u7528\u6765\u63a7\u5236\u8f93\u51fa\u5230\u6d4f\u89c8\u5668\u7684\u754c\u9762\u89c6\u56fe\u5bf9\u8c61\u3002"]}),"\n",(0,i.jsxs)(n.h3,{id:"53101-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.10.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {7,9,11-37}",children:'using Microsoft.AspNetCore.Mvc;\r\nusing Microsoft.AspNetCore.Mvc.Controllers;\r\nusing Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyResultFilter : IAsyncResultFilter\r\n{\r\n public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)\r\n {\r\n // \u83b7\u53d6\u63a7\u5236\u5668\u4fe1\u606f\r\n ControllerActionDescriptor? actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;\r\n\r\n // \u83b7\u53d6\u8def\u7531\u8868\u4fe1\u606f\r\n var routeData = context.RouteData;\r\n var controllerName = routeData.Values["controller"];\r\n var actionName = routeData.Values["action"];\r\n var areaName = routeData.DataTokens["area"];\r\n\r\n // \u5224\u65ad\u5982\u679c\u662f MVC \u89c6\u56fe\uff0c\u53ef\u4ee5\u52a8\u6001\u6dfb\u52a0\u6570\u636e\u5230\u9875\u9762\u4e2d\r\n if (context.Result is ViewResult viewResult)\r\n {\r\n // \u52a8\u6001\u6dfb\u52a0\u6570\u636e\uff0c\u5728 Razor \u4e2d\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 @TempData["Name"] \u83b7\u53d6\u6570\u636e\u4e86\r\n viewResult.TempData["Name"] = "Furion";\r\n\r\n // \u52a8\u6001\u6dfb\u52a0\u6570\u636e\uff0c\u5728 Razor \u4e2d\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 @ViewBag.Version \u6216 @ViewData["Name"] \u83b7\u53d6\u6570\u636e\u4e86\r\n viewResult.ViewData["Version"] = 1;\r\n }\r\n\r\n // \u8fd9\u91cc\u8fd8\u53ef\u4ee5\u5f3a\u5236\u6027\u6362\u6389 Result\r\n // context.Result = new ContentResult("....");\r\n\r\n // \u6267\u884c\u4e0b\u4e00\u4e2a\u7ed3\u679c\u8fc7\u6ee4\u5668\uff0c\u5982\u679c\u76f4\u63a5\u77ed\u8def\u8fd4\u56de\uff0c\u53ef\u8bbe\u7f6e context.Cancel = true; \u8fd9\u6837\u5c31\u4e0d\u4f1a\u6267\u884c\u4e0b\u4e00\u4e2a\u8fc7\u6ee4\u5668\uff0c\u8fd9\u4e2a\u548c\u4e0b\u5217\u4ee3\u7801\u662f\u4e92\u65a5\u7684\r\n var resultContext = await next();\r\n\r\n // \u83b7\u53d6\u8fd4\u56de\u7684\u7ed3\u679c\r\n var result = resultContext.Result;\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"[TypeFilter]"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[TypeFilter(typeof(MyResultFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"[ServiceFilter]"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Starup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[ServiceFilter(typeof(MyResultFilter))]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"53102-resultfilterattribute-\u65b9\u5f0f",children:["5.3.10.2 ",(0,i.jsx)(n.code,{children:"ResultFilterAttribute"})," \u65b9\u5f0f"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {5,7}",children:"using Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication4.Filters;\r\n\r\npublic class MyResultAttribute : ResultFilterAttribute\r\n{\r\n public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u53c2\u8003\u63a5\u53e3\u65b9\u5f0f\r\n\r\n return base.OnResultExecutionAsync(context, next);\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709\u7684\u63a7\u5236\u5668/ ",(0,i.jsx)(n.code,{children:"Action"}),"\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add();\r\n});\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u66f4\u7b80\u6613\u65b9\u5f0f",type:"tip",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86 ",(0,i.jsx)(n.code,{children:"services.AddMvcFilter()"}),"\uff0c\u53ef\u4f7f\u7528\u5b83\u4ee3\u66ff\u4e0a\u9762\u591a\u884c\u6ce8\u518c\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyResult]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"53103-\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\u7ec4\u5408",children:["5.3.10.3 ",(0,i.jsx)(n.code,{children:"\u7279\u6027\u5b9a\u4e49\u65b9\u5f0f\uff08\u7ec4\u5408\uff09"})]}),"\n",(0,i.jsxs)(n.p,{children:["\u53ea\u9700\u8981\u4e0a\u8ff0\u63a5\u53e3\u65b9\u5f0f\u4e2d\u6dfb\u52a0\u6d3e\u751f ",(0,i.jsx)(n.code,{children:"Attribute"})," \u5e76\u8bbe\u7f6e ",(0,i.jsx)(n.code,{children:"[AttributeUsage]"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,4}",children:"[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]\r\npublic class MyResultFilterAttribute : Attribute, IAsyncResultFilter\r\n{\r\n public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u540c\u4e0a\r\n }\r\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"\u4f7f\u7528\uff1a"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyResultFilter]\r\npublic IActionResult Get()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"53104-ialwaysrunresultfilter",children:["5.3.10.4 ",(0,i.jsx)(n.code,{children:"IAlwaysRunResultFilter"})]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"IAlwaysRunResultFilter"})," \u548c ",(0,i.jsx)(n.code,{children:"IAsyncAlwaysRunResultFilter"})," \u63a5\u53e3\u58f0\u660e\u4e86\u4e00\u4e2a\u9488\u5bf9\u6240\u6709\u64cd\u4f5c\u7ed3\u679c\u8fd0\u884c\u7684 ",(0,i.jsx)(n.code,{children:"IResultFilter"})," \u5b9e\u73b0\u3002 \u8fd9\u5305\u62ec\u7531\u4ee5\u4e0b\u5bf9\u8c61\u751f\u6210\u7684\u64cd\u4f5c\u7ed3\u679c\uff1a"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"\u8bbe\u7f6e\u77ed\u8def\u7684\u6388\u6743\u7b5b\u9009\u5668\u548c\u8d44\u6e90\u7b5b\u9009\u5668\u3002"}),"\n",(0,i.jsx)(n.li,{children:"\u5f02\u5e38\u7b5b\u9009\u5668\u3002"}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u8be6\u7ec6\u4f7f\u7528\u53ef\u67e5\u770b\u5fae\u8f6f\u5b98\u65b9\u6587\u6863 ",(0,i.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0#ialwaysrunresultfilter-and-iasyncalwaysrunresultfilter",children:"https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0#ialwaysrunresultfilter-and-iasyncalwaysrunresultfilter"})]}),"\n",(0,i.jsxs)(n.h2,{id:"5311-razorpages-\u7b5b\u9009\u5668",children:["5.3.11 ",(0,i.jsx)(n.code,{children:"RazorPages \u7b5b\u9009\u5668"})]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Razor Pages"})," \u7b5b\u9009\u5668\u4ec5\u652f\u6301 ",(0,i.jsx)(n.code,{children:"Razor Pages"})," \u9879\u76ee\u7c7b\u578b\u3002"]}),"\n",(0,i.jsxs)(n.h3,{id:"53111-\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f",children:["5.3.11.1 ",(0,i.jsx)(n.code,{children:"\u63a5\u53e3\u5b9a\u4e49\u65b9\u5f0f"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {5,13,14-24,32}",children:'using Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication1;\r\n\r\npublic class MyPageFilter : IAsyncPageFilter\r\n{\r\n /// \r\n /// \u8c03\u7528\u65b9\u6cd5\u4e4b\u524d\r\n /// \r\n /// \r\n /// \r\n /// \r\n public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)\r\n {\r\n // \u8def\u7531\u4fe1\u606f\r\n var routeData = context.RouteData;\r\n\r\n // \u8bf7\u6c42\u65b9\u6cd5\u4fe1\u606f\r\n var actionDescriptor = context.ActionDescriptor;\r\n\r\n // \u5904\u7406\u65b9\u6cd5\u4fe1\u606f\r\n var methodDescriptor = context.HandlerMethod;\r\n\r\n await next.Invoke();\r\n }\r\n\r\n /// \r\n /// \u6a21\u578b\u7ed1\u5b9a\u4e4b\u524d\r\n /// \r\n /// \r\n /// \r\n public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)\r\n {\r\n return Task.CompletedTask;\r\n }\r\n}\n'})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5168\u5c40\u6ce8\u518c"})}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"ConfigureService"})," \u4e2d\u6ce8\u518c\uff0c\u8be5\u65b9\u5f0f\u4f1a\u4f5c\u7528\u6240\u6709 ",(0,i.jsx)(n.code,{children:"Page Handler"})]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3}",children:"services.Configure(options =>\r\n{\r\n options.Filters.Add(new MyPageFilter());\r\n});\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"53112-resultfilterattribute-\u65b9\u5f0f",children:["5.3.11.2 ",(0,i.jsx)(n.code,{children:"ResultFilterAttribute"})," \u65b9\u5f0f"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {5,7}",children:"using Microsoft.AspNetCore.Mvc.Filters;\r\n\r\nnamespace WebApplication1.Filters;\r\n\r\npublic class MyResultAttribute : ResultFilterAttribute\r\n{\r\n public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)\r\n {\r\n // \u4ee3\u7801\u53c2\u8003\u63a5\u53e3\u65b9\u5f0f\r\n\r\n return base.OnResultExecutionAsync(context, next);\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.code,{children:"\u5c40\u90e8\u7279\u6027\u65b9\u5f0f"})}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyResult]\r\npublic void OnGet()\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.h2,{id:"5312-\u7b5b\u9009\u5668\u53d6\u6d88\u548c\u77ed\u8def",children:"5.3.12 \u7b5b\u9009\u5668\u53d6\u6d88\u548c\u77ed\u8def"}),"\n",(0,i.jsxs)(n.p,{children:["\u901a\u5e38\u7b5b\u9009\u5668\u652f\u6301\u591a\u4e2a\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u8981\u8c03\u7528 ",(0,i.jsx)(n.code,{children:"await next()"})," \u65b9\u6cd5\u90fd\u4f1a\u8fdb\u53bb\u4e0b\u4e00\u4e2a\u7b5b\u9009\u5668\uff0c\u4f46\u5982\u679c\u901a\u8fc7 ",(0,i.jsx)(n.code,{children:"context.Result = new XXXResult()"})," \u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u5176\u77ed\u8def\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u518d\u6267\u884c\u4e0b\u4e00\u4e2a\u7b5b\u9009\u5668\u3002"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"\u4f46\u4e5f\u6709\u4f8b\u5916"}),"\uff1a"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"IResultFilter"}),"/",(0,i.jsx)(n.code,{children:"IAsyncResultFilter"})," \u7ed3\u679c\u7b5b\u9009\u5668\u4e2d\uff0c\u5219\u4f7f\u7528\u6807\u8bb0 ",(0,i.jsx)(n.code,{children:"context.Cancel = true;"})," \u8bbe\u7f6e\u77ed\u8def\u3002"]}),"\n",(0,i.jsxs)(n.li,{children:["\u5728 ",(0,i.jsx)(n.code,{children:"IExceptionFilter"}),"/",(0,i.jsx)(n.code,{children:"IAsyncExceptionFilter"})," \u5f02\u5e38\u7b5b\u9009\u5668\u4e2d\uff0c\u5219\u4f7f\u7528\u6807\u8bb0 ",(0,i.jsx)(n.code,{children:"context.ExceptionHandled = true;"})," \u8bbe\u7f6e\u77ed\u8def\u3002"]}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"5313-\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f\u63a7\u5236",children:"5.3.13 \u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f\u63a7\u5236"}),"\n",(0,i.jsx)(n.p,{children:"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b5b\u9009\u5668\u662f\u6309\u7167\u4ee5\u4e0b\u6267\u884c\u987a\u5e8f\u6267\u884c\uff1a"}),"\n",(0,i.jsx)(n.h3,{id:"53131-\u4e0d\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",children:"5.3.13.1 \u4e0d\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f"}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"IAuthorizationFilter"})," -> ",(0,i.jsx)(n.code,{children:"IResourceFilter"})," -> ",(0,i.jsx)(n.code,{children:"IActionFilter"})," -> ",(0,i.jsx)(n.code,{children:"IExceptionFilter"})," -> ",(0,i.jsx)(n.code,{children:"IResultFilter"})," -> ",(0,i.jsx)(n.code,{children:"IAlwaysRunResultFilter"})]}),"\n",(0,i.jsx)(n.p,{children:"\u5f02\u6b65\u4e5f\u662f\u5982\u6b64\u3002"}),"\n",(0,i.jsx)(n.h3,{id:"53132-\u76f8\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f",children:"5.3.13.2 \u76f8\u540c\u7c7b\u578b\u7b5b\u9009\u5668\u6267\u884c\u987a\u5e8f"}),"\n",(0,i.jsxs)(n.p,{children:["\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7 ",(0,i.jsx)(n.code,{children:"services.Configure(...)"})," \u65b9\u5f0f\u5148\u6ce8\u518c\u5148\u6267\u884c\uff0c\u4e4b\u540e\u5230\u7279\u6027\u65b9\u5f0f\uff0c\u4e5f\u662f\u91c7\u7528\u5148\u6ce8\u518c\u5148\u6267\u884c\u3002"]}),"\n",(0,i.jsxs)(n.p,{children:["\u5982\u679c\u4f7f\u7528\u540c\u4e00\u79cd\u65b9\u5f0f\uff0c\u5982 ",(0,i.jsx)(n.code,{children:"services.Configuration(...)"})," \u6216\u540c\u4e00\u79cd\u7279\u6027\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u63a7\u5236\u5176\u6267\u884c\u987a\u5e8f\uff0c\u5982\uff1a"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"IOrderedFilter"})," \u65b9\u5f0f\uff1a"]}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3-4}",children:"public class MyActionFilter : IAsyncActionFilter, IOrderedFilter\r\n{\r\n // \u503c\u8d8a\u5927\uff0c\u8d8a\u4f18\u5148\u6267\u884c\r\n public int Order => 1000;\r\n\r\n public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)\r\n {\r\n // ....\r\n }\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"\u7279\u6027"})," \u65b9\u5f0f\uff1a"]}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"[MyActionFilter(Order = 1000)]\r\npublic class ControllerFiltersController : Controller\r\n{\r\n // ...\r\n}\n"})}),"\n",(0,i.jsx)(n.h2,{id:"5314-\u7b5b\u9009\u5668\u4f9d\u8d56\u6ce8\u5165",children:"5.3.14 \u7b5b\u9009\u5668\u4f9d\u8d56\u6ce8\u5165"}),"\n",(0,i.jsxs)(n.p,{children:["\u7b5b\u9009\u5668\u662f\u652f\u6301\u6784\u9020\u51fd\u6570\u4f9d\u8d56\u6ce8\u5165\u670d\u52a1\u7684\uff0c\u4f7f\u7528\u5b83\u4eec\u7684\u524d\u63d0\u662f\u5728 ",(0,i.jsx)(n.code,{children:"Startup.cs"})," \u4e2d\u6ce8\u518c\uff0c\u5982\uff1a"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="Startup.cs"',children:"services.AddScoped();\n"})}),"\n",(0,i.jsx)(n.h2,{id:"5315-\u4e86\u89e3\u66f4\u591a",children:"5.3.15 \u4e86\u89e3\u66f4\u591a"}),"\n",(0,i.jsxs)(n.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a\u7b5b\u9009\u5668\u77e5\u8bc6\u53ef\u67e5\u9605\u5b98\u65b9\u6587\u6863 ",(0,i.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0",children:"https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0"})]}),"\n",(0,i.jsx)(n.h2,{id:"5316-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"5.3.16 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,i.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,i.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,i.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})})]})}function x(e={}){const{wrapper:n}={...(0,s.a)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(u,{...e})}):u(e)}},1151:(e,n,r)=>{r.d(n,{Z:()=>l,a:()=>c});var i=r(7294);const s={},t=i.createContext(s);function c(e){const n=i.useContext(t);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:c(e.components),i.createElement(t.Provider,{value:n},e.children)}}}]);