当前位置: 移动技术网 > IT编程>开发语言>.net > Abp小知识-如何全局设置DontWrapResult属性

Abp小知识-如何全局设置DontWrapResult属性

2019年07月18日  | 移动技术网IT编程  | 我要评论

尤宇生,windows7旗舰版密钥,优胜教育陈昊

demo地址

github相关demo地址:
对demo说明一下,非abp官网模板,使用visual studio 2019创建的asp.net core 2.2 webapi项目,引入abp.aspnetcore包,简单改造一下。

适配abp

简单列举改造步骤:

  1. 初始化项目
  2. 通过nuget引入abp.aspnetcore
  3. 创建module
  4. 改造startup
  5. controller需继承abpcontroller

按照上述步骤改造完成之后,运行站点,返回结果如下(已对返回的json字符串格式化以方便查看)

{
  "result": [
    "value1",
    "value2"
  ],
  "targeturl": null,
  "success": true,
  "error": null,
  "unauthorizedrequest": false,
  "__abp": true
}

wrapresult和dontwrapresult

使用属性wrapresult和dontwrapresult控制是否对controller或者action处理。默认情况下,会对返回结果封装,封装结果如上。[dontwrapresult]等同于[wrapresult(wraponsuccess = false, wraponerror = false)]
对我来说,我是不希望获取干净的返回,不需要什么封装。这时我可以再controller和action上添加[dontwrapresult],屏蔽封装。不想每添加一个controller或者action,都添加该属性?你需要全局配置configuration.modules.abpaspnetcore()
配置如下

var result = new dontwrapresultattribute();
configuration.modules.abpaspnetcore().defaultwrapresultattribute.wraponerror = result.wraponerror;
configuration.modules.abpaspnetcore().defaultwrapresultattribute.wraponsuccess = result.wraponsuccess;

详情可查看:如何全局配置wrapresult

全局配置之后,返回如下

[
  "value1",
  "value2"
]

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网