{"id":1179,"date":"2022-05-13T11:50:48","date_gmt":"2022-05-13T03:50:48","guid":{"rendered":"http:\/\/www.wayln.com\/?p=1179"},"modified":"2022-05-13T19:52:28","modified_gmt":"2022-05-13T11:52:28","slug":"%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=1179","title":{"rendered":"\u4f9d\u8d56\u6ce8\u5165"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u4f9d\u8d56\u6ce8\u5165\u670d\u52a1\u5b9a\u4f4d<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u5728\u4e2d\u95f4\u4ef6\u4e2d\u4f7f\u7528\u670d\u52a1<\/a><\/li><li><a href=\"#Endpoint\"><span class=\"toc_number toc_depth_1\">3<\/span> \u5728Endpoint\u4e2d\u4f7f\u7528\u670d\u52a1<\/a><ul><li><a href=\"#HttpContext\"><span class=\"toc_number toc_depth_2\">3.1<\/span> \u4eceHttpContext\u5bf9\u8c61\u4e2d\u83b7\u53d6\u670d\u52a1<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_2\">3.2<\/span> \u4f7f\u7528\u4e00\u4e2a\u9002\u914d\u51fd\u6570<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u4f9d\u8d56\u6ce8\u5165\u670d\u52a1\u5b9a\u4f4d<\/span><\/h1>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u4f20\u9012\u5230\u7a0b\u5e8f\u9700\u8981\u7684\u90e8\u5206<\/li>\n<li>\u5728\u670d\u52a1\u7c7b\u6dfb\u52a0\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u63d0\u4f9b\u76f4\u63a5\u8bbf\u95ee\u5171\u4eab\u7684\u5b9e\u4f8b<\/li>\n<\/ol>\n<h1><span id=\"i-2\">\u5728\u4e2d\u95f4\u4ef6\u4e2d\u4f7f\u7528\u670d\u52a1<\/span><\/h1>\n<pre><code class=\"language-CSharp line-numbers\">public class WeatherMiddleware{\n    private RequestDelegate next;\n    private IRespinseFormatter formatter;\n    public WeatherMiddleware(RequestDelegate nextDelegate,IResponseFormatter respFormatter)\n    {\n        next=nextDelegate;\n        formatter=respFormatter;\n    }\n    public async Task Invoe(HttpContext context){\n        if(context.Request.Path==\"\/middleware\/class\")\n        {\n            await formatter.Format(context,\"Middleware class:It is raining in London\");\n            else\n            {\n                await next(context);\n            }\n        }\n    }\n}\n\n\/\/\u5728Program.cs\u4e2d\u4f7f\u7528\nbuilder.Services.AddSingleton&lt;IResponseFormatter,HtmlResponseFormatter&gt;();\napp.UseMiddleware&lt;WatherMiddleware&gt;();\n<\/code><\/pre>\n<h1><span id=\"Endpoint\">\u5728Endpoint\u4e2d\u4f7f\u7528\u670d\u52a1<\/span><\/h1>\n<p>\u6709\u7684\u7aef\u70b9\u7c7b\u4e2d\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4e14\u662f\u9759\u6001\u7684\uff0c\u60c5\u51b5\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u529e\u6cd5\u4f7f\u7528\u4f9d\u8d56<\/p>\n<h2><span id=\"HttpContext\">\u4eceHttpContext\u5bf9\u8c61\u4e2d\u83b7\u53d6\u670d\u52a1<\/span><\/h2>\n<pre><code class=\"language-csharp line-numbers\">public static async Task Endpoint(HttpContext context)\n{\n    IResponseFormatter formatter=context.RequestServices.GetRequiredService&lt;IResponseFormatter&gt;();\n    await formatter.Format(context,\"Endpoint Class:It is cloudy in Milan\");\n}\n<\/code><\/pre>\n<h2><span id=\"i-3\">\u4f7f\u7528\u4e00\u4e2a\u9002\u914d\u51fd\u6570<\/span><\/h2>\n<p>\u53ef\u4ee5\u5728\u7aef\u70b9\u65b9\u6cd5\u4f7f\u7528\u63a5\u53e3<\/p>\n<pre><code class=\"language-CSharp line-numbers\">public class WeatherEndpoint{\n    public static async Task Endpoint(HttpContext context,IResponseFormatter formatter)\n{\n    await formatter.Format(context,\"Endpoint Class:It is cloudy in Milan\");\n}\n}\n\npublic static class EndpointExtensions{\n    public static void MapWeather(this IEndpointRouteBuilder app,string path)\n    {\n        IResponseFormatter formatter=app.ServiceProvider.GetRequiredService&lt;IResponseFormatter&gt;();\n        app.MapGet(path,context=&gt;Platform.WeatherEndpoint.Endpoint(context,formatter));\n    }\n}\n\napp.MapWeater(\"endpoint\/class\");\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u4f9d\u8d56\u6ce8\u5165\u670d\u52a1\u5b9a\u4f4d2 \u5728\u4e2d\u95f4\u4ef6\u4e2d\u4f7f\u7528\u670d\u52a13 \u5728Endpoint\u4e2d\u4f7f\u7528\u670d\u52a13.1 \u4eceHttpC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,4,2],"tags":[],"class_list":["post-1179","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-c","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1179"}],"version-history":[{"count":4,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1179\/revisions"}],"predecessor-version":[{"id":1181,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1179\/revisions\/1181"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}