简体   繁体   English

如何处理asp.net核心中的异常“无效文件签名”?

[英]how to handle an exception "Invalid file signature" in asp.net core?

I am using .txt file instead of using excel file so I should be getting 400 error but I am getting 500 error.我使用 .txt 文件而不是使用 excel 文件,所以我应该得到 400 错误,但我得到 500 错误。 I want to catch the exception and send a 400 response code with an appropriate response body.我想捕获异常并发送带有适当响应正文的 400 响应代码。

[Route("file/")]
[AuthorizeFunction(AuthFunctions.Schema.Create)]
[HttpPost]
[ResponseType(typeof(Schema))]
public async Task<IActionResult> Create([FromBody] Guid fileId)
{
     var result = await _SchemaService.Create(fileId);
     return Created("GetSchema", new { id = result.Id }, result);
}

You can use this code to catch specific error您可以使用此代码来捕获特定错误

[Route("file/")]
[AuthorizeFunction(AuthFunctions.Schema.Create)]
[HttpPost]
[ResponseType(typeof(Schema))]
public async Task<IActionResult> Create([FromBody] Guid fileId)
{
     try {
         var result = await _SchemaService.Create(fileId);
         return Created("GetSchema", new { id = result.Id }, result);
     }
     catch (Exception exc){
       if (exc.GetType().FullName == "Your_Exception_Name") 
       {
          // Check your exception name here
       }
   }
}

or或者

catch(Exception ex)
{
  if(ex.InnerException is ExceptionInstance)// exception instance type you want to check
  {

  }
}

Update更新

You can just use catch(Exception ex) for general exception then return BadRequest()您可以只使用catch(Exception ex)进行一般异常,然后返回BadRequest()

catch(Exception ex)
{
  return BadRequest();
}

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

相关问题 如何在 asp.net 内核的基础 controller 中处理 On Exception? - How to handle On Exception in the base controller of asp.net core? ASP.NET 核心:如果数据库未连接或不存在则处理异常 - ASP.NET Core : handle exception if database is not connected or does not exist 如何在Asp.net Core中手动验证JWT签名 - How to verify JWT signature manually in Asp.net Core Asp.Net Core Azure AD V1.0 JWT身份验证无效签名 - Asp.Net Core Azure AD V1.0 JWT Authentication Invalid Signature ASP.NET Core WebAPI: Bearer error=&quot;invalid_token&quot;, error_description=&quot;未找到签名密钥&quot; - ASP.NET Core WebAPI: Bearer error="invalid_token", error_description="The signature key was not found" ASP.NET Core 3.1 JWT 签名在使用 AddJwtBearer() 时无效 - ASP.NET Core 3.1 JWT signature invalid when using AddJwtBearer() 使用超链接在asp.net中下载文件时处理异常 - Handle exception when using hyperlink for downloading a file in asp.net 如何在 ASP.NET Core 中下载文件? - How to download a file in ASP.NET Core? 如何在 ASP.NET Core 中处理跨站点脚本 - How to handle cross site scripting in ASP.NET Core 如何处理 ASP.NET Core MVC 中的模型绑定异常? - How to handle model binding exceptions in ASP.NET Core MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM