Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 在ASP.NET核心异常筛选器中获取请求/模型正文而不重播_Asp.net Core_Exception - Fatal编程技术网

Asp.net core 在ASP.NET核心异常筛选器中获取请求/模型正文而不重播

Asp.net core 在ASP.NET核心异常筛选器中获取请求/模型正文而不重播,asp.net-core,exception,Asp.net Core,Exception,我有几个模型共享一个界面,看起来像 public interface IRequest { public string RequestId { get; set; } } public class CatController: ControllerBase { [HttpPost] public IActionResult AddCat([FromBody] cat) { // call database and add cat to db }

我有几个模型共享一个界面,看起来像

public interface IRequest {
    public string RequestId { get; set; }
}
public class CatController: ControllerBase {
    [HttpPost]
    public IActionResult AddCat([FromBody] cat) {
        // call database and add cat to db
    }
}
public void OnException(ExceptionContext context) {
    // create a proper error response message
    var error = new ErrorResponse() {
        ResponseCode = 400,
        RequestId = 2, // HERE IS THE SPOT, I WANT TO READ RequestId from the RequestBody or Model without re-reading the request

        // The solution below works, but only when it is a custom exception where I had previously manually added the Data entry
        // RequestId = context.Exception.Data["RequestId"]?.ToString()
    };
    context.Result = new JsonResult(error);
}
实现此功能的模型示例如下所示

public class CatRequest : IRequest {
    public string RequestId { get; set; }
    public string Name { get; set; }
我有一个
CatController
,看起来像

public interface IRequest {
    public string RequestId { get; set; }
}
public class CatController: ControllerBase {
    [HttpPost]
    public IActionResult AddCat([FromBody] cat) {
        // call database and add cat to db
    }
}
public void OnException(ExceptionContext context) {
    // create a proper error response message
    var error = new ErrorResponse() {
        ResponseCode = 400,
        RequestId = 2, // HERE IS THE SPOT, I WANT TO READ RequestId from the RequestBody or Model without re-reading the request

        // The solution below works, but only when it is a custom exception where I had previously manually added the Data entry
        // RequestId = context.Exception.Data["RequestId"]?.ToString()
    };
    context.Result = new JsonResult(error);
}
我非常清楚在数据库操作中可能会出现一些问题,因此我有一个全局异常过滤器,它可以正常工作

public interface IRequest {
    public string RequestId { get; set; }
}
public class CatController: ControllerBase {
    [HttpPost]
    public IActionResult AddCat([FromBody] cat) {
        // call database and add cat to db
    }
}
public void OnException(ExceptionContext context) {
    // create a proper error response message
    var error = new ErrorResponse() {
        ResponseCode = 400,
        RequestId = 2, // HERE IS THE SPOT, I WANT TO READ RequestId from the RequestBody or Model without re-reading the request

        // The solution below works, but only when it is a custom exception where I had previously manually added the Data entry
        // RequestId = context.Exception.Data["RequestId"]?.ToString()
    };
    context.Result = new JsonResult(error);
}
我可以得到更好的实现吗


谢谢。

如果您只想处理控制器操作中发生的异常,使用异常过滤器来实现它是可以的

此外,还可以使用中间件进行全局错误处理


有关“处理ASP.NET Core中的错误”的详细信息,请检查:

如果您只想处理控制器操作中发生的异常,请使用异常过滤器来实现它

此外,还可以使用中间件进行全局错误处理

有关“处理ASP.NET Core中的错误”的详细信息,请检查: