如何为ABP中的默认异常响应增加值? [英] How to add value to the default exception response in ABP?

查看:251
本文介绍了如何为ABP中的默认异常响应增加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在异常中添加一个ID(GUID),并且:

I want to add an ID (GUID) to the exception and:

  1. 登录
  2. 将其返回给客户端json响应

我应该在哪里生成此日志ID值,并将其添加到所记录的异常消息中.以及在哪里更改以下默认响应?

Where should I generate this log ID value and add it to the exception message that is logged. And where to change the following default response?

{
  "targetUrl": null,
  "result": null,
  "success": false,
  "error": {
    "message": "An internal error occurred during your request!",
    "details": "..."
  },
  "unAuthorizedRequest": false
}

我正在使用.NET Core版本.

I am using .NET Core version.

推荐答案

如果要禁用显示特定AJAX呼叫的消息,请在abp.ajax选项中添加abpHandleError: false.

If you want to disable displaying the message for a particular AJAX call, add abpHandleError: false into the abp.ajax options.

或者您可以禁用框架异常包装器的默认行为

Or you can disable the default behavior of the framework exception wrapper

public class PeopleController : AbpController
{
    [HttpPost]
    [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
    public JsonResult SavePerson(SavePersonModel person)
    {
        //TODO: save new person to database and return new person's id
        return Json(new {PersonId = 42});
    }
}

https://aspnetboilerplate .com/Pages/Documents/Javascript-API/AJAX?searchKey = wrap#asp-net-mvc-controllers

另一件事是;您可以通过以下配置将异常详细信息发送给客户端

Another thing is; you can send exception details to the client by the below configuration

...
using Abp.Web.Configuration;
...
public override void PreInitialize() 
{
    Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}
...

https://aspnetboilerplate.com/Pages/Startup-Configuration#configuring-modules

结果包装和异常处理:

如果成功执行了动作,则默认情况下,ASP.NET Boilerplate不会包装Web API动作.但是,它处理和包装异常.您可以将WrapResult/DontWrapResult属性添加到操作和控制器,以进行更好的控制.您可以从启动配置中更改此默认行为(使用Configuration.Modules.AbpWebApi()...).有关结果包装的更多信息,请参见AJAX文档.

ASP.NET Boilerplate does not wrap Web API actions by default if an action has successfully executed. It, however, handles and wraps exceptions. You can add the WrapResult/DontWrapResult attributes to actions and controllers for finer control. You can change this default behavior from the startup configuration (using Configuration.Modules.AbpWebApi()...). See the AJAX document for more info about result wrapping.

https://aspnetboilerplate .com/Pages/Documents/Web-API-Controllers?searchKey = wrap#result-wrapping-exception-handling

包装结果

ASP.NET Boilerplate使用AjaxResponse对象包装动态Web API操作的返回值.有关包装的更多信息,请参见ajax文档.您可以按方法或按应用程序服务启用/禁用包装.请参阅以下示例应用程序服务:

ASP.NET Boilerplate wraps the return values of dynamic Web API actions using an AjaxResponse object. See the ajax documentation for more information on wrapping. You can enable/disable wrapping per method or per application service. See this example application service:

public interface ITestAppService : IApplicationService
{
    [DontWrapResult]
    DoItOutput DoIt(DoItInput input);
}

https://aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API?searchKey = wrap#wrapping-results

最后,您可以编写自己的ResultWrapperHandler ...

Lastly you can write your own ResultWrapperHandler...

public class CustomResultWrapperHandler : ResultWrapperHandler, ITransientDependency
{

    //...
    protected override void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
    {

        //...

        base.WrapResultIfNeeded(request, response);
    }
}

这篇关于如何为ABP中的默认异常响应增加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆