简体   繁体   English

如何为 ABP 中的默认异常响应添加值?

[英]How to add value to the default exception response in ABP?

I want to add an ID (GUID) to the exception and:我想向异常添加一个 ID (GUID) 并且:

  1. Log it记录它
  2. Return it to the client json response将其返回给客户端 json 响应

Where should I generate this log ID value and add it to the exception message that is logged.我应该在哪里生成此日志 ID 值并将其添加到记录的异常消息中。 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
}

I am using .NET Core version.我正在使用 .NET Core 版本。

If you want to disable displaying the message for a particular AJAX call, add abpHandleError: false into the abp.ajax options. 如果要禁用显示特定AJAX调用的消息,请在abp.ajax选项中添加abpHandleError: false

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 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 https://aspnetboilerplate.com/Pages/Startup-Configuration#configuring-modules


Result Wrapping & Exception Handling: 结果包装和异常处理:

ASP.NET Boilerplate does not wrap Web API actions by default if an action has successfully executed. 如果成功执行了动作,则默认情况下,ASP.NET Boilerplate不会包装Web API动作。 It, however, handles and wraps exceptions. 但是,它处理和包装异常。 You can add the WrapResult/DontWrapResult attributes to actions and controllers for finer control. 您可以将WrapResult / DontWrapResult属性添加到操作和控制器,以进行更好的控制。 You can change this default behavior from the startup configuration (using Configuration.Modules.AbpWebApi()...). 您可以从启动配置中更改此默认行为(使用Configuration.Modules.AbpWebApi()...)。 See the AJAX document for more info about result wrapping. 有关结果包装的更多信息,请参见AJAX文档。

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


Wrapping Results 包装结果

ASP.NET Boilerplate wraps the return values of dynamic Web API actions using an AjaxResponse object. ASP.NET Boilerplate使用AjaxResponse对象包装动态Web API操作的返回值。 See the ajax documentation for more information on wrapping. 有关包装的更多信息,请参见ajax文档。 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 https://aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API?searchKey=wrap#wrapping-results


Lastly you can write your own ResultWrapperHandler... 最后,您可以编写自己的ResultWrapperHandler ...

public class CustomResultWrapperHandler : ResultWrapperHandler, ITransientDependency
{

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

        //...

        base.WrapResultIfNeeded(request, response);
    }
}

if you want to get special message in some case you can use如果您想在某些情况下获得特殊消息,您可以使用

throw new UserFriendlyException("your message");

the above code just effects on error message and dosnt show the details.上面的代码只影响错误信息,不显示细节。 so its good option for production version.所以它是生产版本的好选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM