项目升级到.net core 2.1
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace COMCMS.Common
|
||||
{
|
||||
@@ -10,19 +12,51 @@ namespace COMCMS.Common
|
||||
/// </summary>
|
||||
public static class MyHttpContext
|
||||
{
|
||||
public static IServiceProvider ServiceProvider;
|
||||
static MyHttpContext()
|
||||
{ }
|
||||
|
||||
public static HttpContext Current
|
||||
private static IHttpContextAccessor _accessor;
|
||||
public static HttpContext Current => _accessor.HttpContext;
|
||||
internal static void Configure(IHttpContextAccessor accessor)
|
||||
{
|
||||
get
|
||||
{
|
||||
object factory = ServiceProvider.GetService(typeof(IHttpContextAccessor));
|
||||
HttpContext context = ((IHttpContextAccessor)factory).HttpContext;
|
||||
return context;
|
||||
}
|
||||
|
||||
_accessor = accessor;
|
||||
|
||||
}
|
||||
|
||||
//public static IServiceProvider ServiceProvider;
|
||||
|
||||
//static MyHttpContext()
|
||||
//{ }
|
||||
|
||||
//public static HttpContext Current
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
|
||||
// object factory = ServiceProvider.GetService<IHttpContextAccessor>();// ServiceProvider.GetService(typeof(IHttpContextAccessor));
|
||||
// HttpContext context = ((IHttpContextAccessor)factory).HttpContext;
|
||||
// return context;
|
||||
// }
|
||||
//}
|
||||
//需要在sartup中加入
|
||||
}
|
||||
|
||||
public static class StaticHttpContextExtensions
|
||||
{
|
||||
|
||||
public static void AddMyHttpContextAccessor(this IServiceCollection services)
|
||||
{
|
||||
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
|
||||
{
|
||||
|
||||
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
|
||||
|
||||
MyHttpContext.Configure(httpContextAccessor);
|
||||
|
||||
return app;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user