欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-04-06 19:09:11
NET Core 拓展方法和中间件集合(支持NET Core2.0+) ......

# Pure.NETCoreExtentensions


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

包含拓展方法:
DistributedCacheExtensions
ConfigurationExtensions
CookiesExtensions
ServiceCollectionExtensions
EnvironmentExtensions
HttpContextExtensions
HttpRequestExtentions
FormFileExtentions
HeaderDictionaryExtensions
DefaultIdentityUserClaimsExtensions
LoggerFactoryExtensions
UrlHelperExtensions
SmtpEmailSenderExtensions
WebHostBuilderExtensions
ApplicationBuilderExtensions

包含中间件:
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!");
            });
        }
        

 

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>

截图