NET Core 拓展方法和中间件集合(支持NET Core2.0+)

Posted ASPNETCore

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NET Core 拓展方法和中间件集合(支持NET Core2.0+)相关的知识,希望对你有一定的参考价值。

https://github.com/purestackorg/Pure.NETCoreExtensions


NET Core 拓展方法和中间件集合(支持NET Core2.0+)

包含中间件:
FriendlyExceptionsMiddleware
htmlMinificationMiddleware
HttpExceptionMiddleware
InternalServerErrorOnExceptionMiddleware
NoServerHttpHeaderMiddleware
ClientRateLimitMiddleware
IpRateLimitMiddleware
StatisticsMiddleware


常用基类:
BaseController
BaseControllerWithIdentity
TokenBucketLimitingService
LeakageBucketLimitingService
Platform

 

使用说明:

1.引用相关包:

<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.4" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.2" />

2.引用命名空间

using Pure.NetCoreExtensions

3.配置 Startup.cs 文件

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=HelloWorld}/{action=Index}/{id?}");
            });            


            //global middleware            app.UseGlobalHostingEnvironment(env)
                .UseGlobalHttpContext()
                .UseGlobalLoggerFactory()
                .UseGlobalErrorHandling()
               ; 

         
            app.Run(async (context) =>
            {                await context.Response.WriteAsync("Hello World!");
            });
        }        

NET Core 拓展方法和中间件集合(支持NET Core2.0+)

 

4.完成 !

 

下面测试代码和截图:

@{
    ViewData["Title"] = "Index";
    Layout = "_Layout";
}
@using Pure.NetCoreExtensions;<h2>Hello World!</h2>@DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")<h3>Config</h3><p>
    ConnectionString: @ConfigurationManager.Configuration.GetConnectionString()</p><h3>AppSetting</h3><p>
    <table>
        <tr>
            <td>Key1</td>
            <td> @ConfigurationManager.AppSettings["Key1"]</td>
        </tr>
        <tr>
            <td>Key2</td>
            <td> @ConfigurationManager.AppSettings["Key2"]</td>
        </tr>
        <tr>
            <td>Key2</td>
            <td> @ConfigurationManager.AppSettings["Key3"]</td>
        </tr>
    </table></p><h3>Env</h3><p>
    <table>
        <tr>
            <td>ApplicationName</td>
            <td> @GlobalHostEnvironment.ApplicationName </td>
        </tr>
        <tr>
            <td>ContentRootPath</td>
            <td> @GlobalHostEnvironment.ContentRootPath </td>
        </tr>
        <tr>
            <td>EnvironmentName</td>
            <td> @GlobalHostEnvironment.EnvironmentName </td>
        </tr>
        <tr>
            <td>WebRootPath</td>
            <td> @GlobalHostEnvironment.WebRootPath </td>
        </tr>

    </table></p><h3>Platform</h3><p>
    <table>
        <tr>
            <td>OS</td>
            <td> @Platform.OS </td>
        </tr>
        <tr>
            <td>Is64BitOperatingSystem</td>
            <td> @Platform.Is64BitOperatingSystem </td>
        </tr>
        <tr>
            <td>OSDescription</td>
            <td> @Platform.OSDescription </td>
        </tr>
        <tr>
            <td>OSArchitecture</td>
            <td> @Platform.OSArchitecture </td>
        </tr>
        <tr>
            <td>ProcessArchitecture</td>
            <td> @Platform.ProcessArchitecture </td>
        </tr>
        <tr>
            <td>RuntimeType</td>
            <td> @Platform.RuntimeType </td>
        </tr>


    </table></p><h3>HttpContext</h3><p>
    <table>
        <tr>
            <td>Current</td>
            <td> @GlobalHttpContext.Current   </td>
        </tr>
        <tr>
            <td>Request.GetClientIpAddress </td>
            <td> @GlobalHttpContext.Current.Request.GetClientIpAddress()   </td>
        </tr>
        <tr>
            <td>Request.IsLocalRequest </td>
            <td> @GlobalHttpContext.Current.Request.IsLocalRequest()   </td>
        </tr>
        <tr>
            <td>Request.GetConnectionId </td>
            <td> @GlobalHttpContext.Current.Request.GetConnectionId()   </td>
        </tr>
        <tr>
            <td>Request.ContentLength </td>
            <td> @GlobalHttpContext.Current.Request.GetRequestId()   </td>
        </tr>
        <tr>
            <td>Request.GetUserAgent </td>
            <td> @GlobalHttpContext.Current.Request.GetUserAgent()   </td>
        </tr>

        <tr>
            <td>Response.ContentLength </td>
            <td> @GlobalHttpContext.Current.Response.ContentLength   </td>
        </tr>
        <tr>
            <td>Response.ContentType </td>
            <td> @GlobalHttpContext.Current.Response.ContentType   </td>
        </tr>

        <tr>
            <td>Response.StatusCode </td>
            <td> @GlobalHttpContext.Current.Response.StatusCode   </td>
        </tr>
    </table></p>



以上是关于NET Core 拓展方法和中间件集合(支持NET Core2.0+)的主要内容,如果未能解决你的问题,请参考以下文章

让ASP.NET Core支持GraphQL - 手写中间件

ASP.NET Core 6.0对热重载的支持

.Net Core Cors中间件解析

Asp.Net Core 7 preview 4 重磅新特性--限流中间件

ASP.NET Core中的缓存[1]:如何在一个ASP.NET Core应用中使用缓存

一个.Net Core开源缓存中间件,让你更加简单方便使用缓存