ASP.NET Core 5控制器里面获取注册的实例接口

这个博客主要介绍了ASP.NET Core中控制器基类的使用,包括获取当前登录用户、客户端IP的方法。同时展示了如何通过依赖注入获取ILeaderBLL接口的实例,以及查询领导人列表的示例代码。在实际操作中,例如在`DelPc`方法中,展示了如何利用依赖注入删除数据并重新加载领导人数据。
摘要由CSDN通过智能技术生成

控制器基类参考

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.IBLL;
using WebNetCore5_Img_Storage.Model;

namespace WebNetCore5_Img_Storage.Controllers
{
    /// <summary>
    /// 控制器基类,提供基础数据,基础操作
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        protected UserView CurrentUser
        {
            get
            {
                string sessionJson = HttpContext.Session.GetString("user");
                UserView userView = null;
                try
                {
                    userView = System.Text.Json.JsonSerializer.Deserialize<UserView>(sessionJson);
                }
                catch (Exception)
                {
                    userView = null;
                }
                return userView;
            }
        }

        /// <summary>
        /// 获取客户端ip
        /// </summary>
        protected string GetCustomerIp
        {
            get
            {
                var ip = this.Request.Headers["X-Forwarded-For"].FirstOrDefault();
                if (string.IsNullOrEmpty(ip))
                {
                    ip = this.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
                }
                return ip;
            }
        }


        /// <summary>
        /// 领导人列表,查询最近20条
        /// </summary>
        /// <returns></returns>
        protected async Task LeaderXmSelectData()
        {
            ILeaderBLL leaderBLL = (ILeaderBLL)HttpContext.RequestServices.GetService(typeof(ILeaderBLL));
            var pageLeader = await leaderBLL.PageAsync(null, 1, 20);
            HashSet<string> leaderArr = new HashSet<string>();
            if (pageLeader.List?.Count == 0)
            {
                ViewBag.pageLeader = "[]";
            }
            else
            {
                foreach (var item in pageLeader.List)
                {
                    string name = item.Department_name + "." + item.Job_name + "." + item.Real_name;
                    string id = item.Id;
                    leaderArr.Add("{\"name\":\"" + name + "\",\"value\":\"" + id + "\"}");
                }
                ViewBag.pageLeader = "[" + string.Join(",", leaderArr) + "]";
            }
        }

    }
}

在控制器中获取注册的实例

//ajax 删除提交的批次,id为批次id
[LoginFilter("3000301")]
public async Task<IActionResult> DelPc(string id)
{
	var result = await checkImgBLL.DeletePc(id, CurrentUser);
	
	//获取注册的接口
	ILeaderBLL leaderBLL = (ILeaderBLL)HttpContext.RequestServices.GetService(typeof(ILeaderBLL));
	var pageLeader = await leaderBLL.PageAsync(null, 1, 20);

	return Json(result);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值