• asp.net core在其他程序集获取HttpContext


    首先在Program.cs中,注册

    builder.Services.AddHttpContextAccessor();
    
    • 1

    Program.cs完整代码:

    using Microsoft.AspNetCore.Mvc.Filters;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using System.Text.Encodings.Web;
    using System.Text.Unicode;
    using WebReportInfoNetCore7;
    using WebReportInfoNetCore7.DataBaseEntity;
    using WebReportInfoNetCore7.IBLL;
    using WebReportInfoNetCore7.IBLL_impl;
    using WebReportInfoNetCore7.IDAO;
    using WebReportInfoNetCore7.IDAO_impl;
    
    var builder = WebApplication.CreateBuilder(args);
    
    //依赖注入
    builder.Services.AddSingleton<IAh_userDao, Ah_userDao>();
    builder.Services.AddSingleton<IUserBll, UserBll>();
    
    
    // Add services to the container.
    builder.Services.AddControllersWithViews(ops => {
        //全局异常过滤器,注册
        ops.Filters.Add<ExceptionFilter>();
    })
    
    //Microsoft.AspNetCore.Mvc.NewtonsoftJson
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });
    
     
    builder.Services.AddSession();
    
    //IHttpContextAccessor 在其他程序集中获取HttpContext
    builder.Services.AddHttpContextAccessor();
    
    //解决输入页面中文被编码了
    builder.Services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
    
    var app = builder.Build();
    
     
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    app.UseSession();
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    
    AppServicesHelpter.App = app;
    
    
    //Microsoft.AspNetCore.Session.ISessionStore
    //IHttpContextAccessor Accessor = app.Services.GetService();
    
    app.Run();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    其他程序集中获取,其他程序集需要引用文件:Microsoft.AspNetCore.Http.Abstractions.dll
    文件位置:
    C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\7.0.5\ref\net7.0

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.AspNetCore.Http;
    using WebReportInfoNetCore7.ViewEntity;
    using System.Net.Http;
    using System.Runtime.CompilerServices;
    
    namespace WebReportInfoNetCore7.DataBaseEntity
    {
        /// 
        /// 依赖注册,获取接口实例
        /// 
        /// 创建时间:2023-10-8 16:03:39 
        public sealed class AppServicesHelpter
        {
            /// 
            /// 程序
            /// 
            public static Microsoft.AspNetCore.Builder.WebApplication App { get; set; }
    
            /// 
            /// 获取接口实例
            /// 
            /// 
            /// 
            public static T GetServices<T>() where T : notnull
            {
                T instance = App.Services.GetRequiredService<T>();
                return instance;
            }
    
            /// 
            /// 获取当前登录用户
            /// 
            /// 
            /// 创建时间:2023-10-10 14:48:23, 
            public static Ah_userView CurrentLoginUser()
            {
                Microsoft.AspNetCore.Http.IHttpContextAccessor accessor = App.Services.GetService<Microsoft.AspNetCore.Http.IHttpContextAccessor>();
                string json = accessor.HttpContext.Session.GetString("userLogin");
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Ah_userView>(json);
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    微软文档:
    https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/http-context?view=aspnetcore-7.0

  • 相关阅读:
    Etcd教程 — 第六章 Etcd之核心API V3
    javaScript操作数组的方法
    每天3分钟,重学ES6-ES12(十五)异步代码处理方案
    一文搞懂前端兼容问题
    静态住宅代理是什么?为什么要选择它?
    GBase 8c 核心技术简介(中)
    关闭jenkins插件提醒信息
    线下活动|来开源集市和Jina AI面对面say hi!
    8月初整理,Adobe 2022全家桶更新情况(Mac+win)限时分享
    Linux系统安全
  • 原文地址:https://blog.csdn.net/u011511086/article/details/133750123