我想返回一个403禁止给客户端时,试图执行无效的操作。我需要使用什么方法?
我在网上搜索了一下,但是我只找到了这些 MVC 5:
如果 web api 方法的返回类型是 HttpResponseMessage,则 你需要使用以下代码:
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "RFID is disabled for this site."); Or if the return type for your web api method is IHttpActionResult then you need to use the below code return StatusCode(HttpStatusCode.Forbidden,"RFID is disabled for this site.");
如何为 IActionResult 类型返回403:
public IActionResult Put(string userid, [FromBody]Setting setting) { var result = _SettingsRepository.Update(userid, setting); if (result == true) { return Ok(201); } else { return BadRequest(); } }