# 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;Hello World!
@DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")Config
ConnectionString: @ConfigurationManager.Configuration.GetConnectionString()
AppSetting
Key1 | @ConfigurationManager.AppSettings["Key1"] |
Key2 | @ConfigurationManager.AppSettings["Key2"] |
Key2 | @ConfigurationManager.AppSettings["Key3"] |
Env
ApplicationName | @GlobalHostEnvironment.ApplicationName |
ContentRootPath | @GlobalHostEnvironment.ContentRootPath |
EnvironmentName | @GlobalHostEnvironment.EnvironmentName |
WebRootPath | @GlobalHostEnvironment.WebRootPath |
Platform
OS | @Platform.OS |
Is64BitOperatingSystem | @Platform.Is64BitOperatingSystem |
OSDescription | @Platform.OSDescription |
OSArchitecture | @Platform.OSArchitecture |
ProcessArchitecture | @Platform.ProcessArchitecture |
RuntimeType | @Platform.RuntimeType |
HttpContext
Current | @GlobalHttpContext.Current |
Request.GetClientIpAddress | @GlobalHttpContext.Current.Request.GetClientIpAddress() |
Request.IsLocalRequest | @GlobalHttpContext.Current.Request.IsLocalRequest() |
Request.GetConnectionId | @GlobalHttpContext.Current.Request.GetConnectionId() |
Request.ContentLength | @GlobalHttpContext.Current.Request.GetRequestId() |
Request.GetUserAgent | @GlobalHttpContext.Current.Request.GetUserAgent() |
Response.ContentLength | @GlobalHttpContext.Current.Response.ContentLength |
Response.ContentType | @GlobalHttpContext.Current.Response.ContentType |
Response.StatusCode | @GlobalHttpContext.Current.Response.StatusCode |
截图