当前位置: 移动技术网 > IT编程>开发语言>.net > NET Core 拓展方法和中间件集合(支持NET Core2.0+)

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

2018年06月15日  | 移动技术网IT编程  | 我要评论

btbook,湖北通城县,乔莎眼霜

# 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>

截图

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网