首页 > 编程语言 >Asp.Net Core Web Api 对于ControllerBase的一些个人习惯扩展

Asp.Net Core Web Api 对于ControllerBase的一些个人习惯扩展

时间:2024-03-02 23:34:02浏览次数:242  
标签:Core Asp Web ControllerBase return static new controllerBase public

ApiResponse类是我定义的返回基类,Code我定的是0成功 不等于0失败,对于Data我习惯于写object类型,不习惯写泛型,我习惯于给ControllerBase写扩展来定义我的返回方法,在写一个类继承于ControllerBase让后控制器再去继承这个类,也是可以的
对于GetUserCode和GetRoleCode是方便获取token中用户的信息

/// <summary>
/// ControllerBase扩展类
/// </summary>
public static class ControllerBaseExtensions
{
    public static ActionResult<ApiResponse> Success(this ControllerBase controllerBase,object data)
    {
        return new ApiResponse()
        {
            Code = 0,
            Data = data,
            Msg = "成功",
            Count = 0
        };
    }
    public static ActionResult<ApiResponse> Success(this ControllerBase controllerBase, object data,int count)
    {
        return new ApiResponse()
        {
            Code = 0,
            Data = data,
            Msg = "成功",
            Count = count
        };
    }
    /// <summary>
    /// 增改删结果
    /// </summary>
    /// <param name="controllerBase"></param>
    /// <param name="audRes">增改删结果</param>
    /// <returns></returns>
    public static ActionResult<ApiResponse> Success(this ControllerBase controllerBase, bool audRes)
    {
        return new ApiResponse()
        {
            Code = audRes ? 0 : 1,
            Data = audRes,
            Msg = audRes ? "成功" : "失败",
        };
    }
    public static ActionResult<ApiResponse> Success(this ControllerBase controllerBase)
    {
        return new ApiResponse()
        {
            Code = 0,
            Data = true,
            Msg = "成功",
            Count = 0
        };
    }

    /// <summary>
    /// 失败
    /// </summary>
    /// <param name="controllerBase"></param>
    /// <returns></returns>
    public static ActionResult<ApiResponse> Failed(this ControllerBase controllerBase,string msg)
    {
        return new ApiResponse()
        {
            Code = 1,
            Data = false,
            Msg = msg,
            Count = 0
        };
    }

    /// <summary>
    /// 从token中获取UserCode
    /// </summary>
    /// <param name="controllerBase"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static string GetUserCode(this ControllerBase controllerBase)
    {
        // 使用controllerBase.HttpContext来获取当前的HttpContext
        var user = controllerBase.HttpContext.User;
        var userCode= user?.FindFirstValue("UserCode");
        if (string.IsNullOrEmpty(userCode)) throw new Exception("userCode为空");
        return userCode;
    }
    /// <summary>
    /// 从token中获取RoleCode
    /// </summary>
    /// <param name="controllerBase"></param>
    /// <returns>RoleCode数组</returns>
    /// <exception cref="Exception"></exception>
    public static string[] GetRoleCode(this ControllerBase controllerBase)
    {
        // 使用controllerBase.HttpContext来获取当前的HttpContext
        var user = controllerBase.HttpContext.User;
        var userCode = user?.FindFirstValue("RoleCode");
        if (string.IsNullOrEmpty(userCode)) throw new Exception("roleCode");
        return userCode.Split(',');
    }

}

标签:Core,Asp,Web,ControllerBase,return,static,new,controllerBase,public
From: https://www.cnblogs.com/cyfj/p/18049445

相关文章

  • JavaWeb_mac_env
    maven安装brew安装wgetbrewinstallwgetbrewcleanup--prune=all //删除所有安装缓存下载mavenwgethttps://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz或者curl-Ohttps://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache......
  • netcore 将图片转为pdf上传
    privatestaticstringGetImageUrl(SaveWeldListInputmodel){varfileUrl=model.ProcedureFiles[0].File_Url;//获取文件扩展名stringextension=Path.GetExtension(fileUrl);stringfilePathDic="";......
  • 探索浏览器录屏Web API 接口的应用前景与限制
    一、浏览器录屏WebAPI接口的优点:简化录屏流程:浏览器录屏WebAPI接口可以直接在网页中调用,无需安装额外的插件或软件,简化了录屏的流程。实时录制与传输:WebAPI接口可以实时录制用户操作并将录屏数据传输到服务器,实现即时的用户行为监测和分析。跨平台兼容性:浏览器录屏W......
  • Taurus.MVC WebMVC 入门开发教程7:部分视图和页面片段(结束篇)
    本系列的目录大纲为:Taurus.MVCWebMVC入门开发教程1:框架下载环境配置与运行Taurus.MVCWebMVC入门开发教程2:一个简单的页面呈现Taurus.MVCWebMVC入门开发教程3:数据绑定ModelTaurus.MVCWebMVC入门开发教程4:数据列表绑定List<Model>Taurus.MVCWebMVC入门开发教程5......
  • web自动化——Selenium 之鼠标操作和按键操作
    一、鼠标操作鼠标是通过使用底层接口执行的,需要调用ActionChains对象来执行对应的方法1、导入ActionChains类包fromselenium.webdriver.common.action_chainsimportActionChains2、ActionChains提供的鼠标操作方法1)clickAndHold它将移动到该元素,然后在给定元素的中......
  • web自动化——Selenium 之下拉
    一、select下拉列表Select类处理select-option1)导入Select类fromselenium.webdriver.support.selectimportSelect2)确保你要操作的元素是select。实例化Select类Select()3)找到select元素对象select_ele=driver.find_element(By.NAME,'cars')4)将sel......
  • IIS10 隐藏 http server、删除 Windows Server IIS 10 和 ASP.NET 中的 HTTP 响应标头
    一、方案1IIS10.0中的removeServerHeaderrequestFiltering在IIS10.0(WindowsServer2016/2019)中,您可以通过在system.webServer节点中配置requestFiltering来移除Server标头:web.config<security><requestFilteringremoveServerHeader="true"/></securit......
  • web自动化——Selenium 之切换
    一、窗口(window)切换在浏览器当中,如果在页面进行了某个操作,结果浏览器打开了另外一个新窗口(tab)。如果要操作新窗口当中的页面元素,就需要窗口切换。前提:有新的窗口打开、或者你要操作其它窗口1、sleep(1)或显示等待--等待浏览器新的窗口出现(无法使用隐性等待,隐性......
  • zabbix-server启动后没有10051端口,web端报错
    1、登入zabbixweb端,报错信息如下 2、登入服务器上查看zabbix-server、zabbix-agent2、apache2等服务都正常运行 3、检查端口情况,发现apache2---80正常、zabbix-agent2---10050正常,只有zabbix-server没有端口信息(图一也是细节那栏也没有localhost:10051) 4、查看zab......
  • 【HMS Core】应用内支付如何退款
    ​【问题描述】应用内支付如何退款? 【解决方案】1、如果您需要帮助用户进行退款(订单退款)操作,建议提单咨询https://developer.huawei.com/consumer/cn/support/feedback/#/2、如果订阅想退款的,可以直接调用撤销订阅或者返还订阅费用来给用户退款。​​https://developer.h......