- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
无论如何我们可以在异常捕获时获取 HttpStatus 代码吗?异常可能是 Bad Request
、408 Request Timeout
、419 Authentication Timeout
?如何在异常 block 中处理?
catch (Exception exception)
{
techDisciplines = new TechDisciplines { Status = "Error", Error = exception.Message };
return this.Request.CreateResponse<TechDisciplines>(
HttpStatusCode.BadRequest, techDisciplines);
}
最佳答案
我注意到您正在捕获一个通用异常。您需要捕获更具体的异常才能获得其独特的属性。在这种情况下,请 try catch HttpException
并检查其状态代码属性。
但是,如果您正在编写服务,则可能需要使用 Request.CreateResponse
来报告错误情况。 http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling有更多信息
关于c# - 如何从 WebAPI 中的异常获取 HttpStatusCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364554/
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); HttpStatusCode statusC
我正在尝试引发 HttpResponseException,如该截屏视频中所述(大约 1 分钟) throw new HttpResponseException(HttpStatusCode.Unau
假设我有以下变量: System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK; 如何检查这是成功状态码还是失败状态码? 例如,我可
我正在编写 XUnit 测试用例。我收到以下错误 Error CS1061 'HttpStatusCode' does not contain a definition for 'Should' an
我正在编写 XUnit 测试用例。我收到以下错误 Error CS1061 'HttpStatusCode' does not contain a definition for 'Should' an
我希望返回 202 - HttpStatusCode.Accepted - 以及一个字符串值。 这怎么写: [HttpPost] public async Task Post() { retu
无论如何我们可以在异常捕获时获取 HttpStatus 代码吗?异常可能是 Bad Request、408 Request Timeout、419 Authentication Timeout?如何在
我正在对一个简单的 Controller 进行单元测试,检查在传入空 ID 时它是否返回 400 代码。但是,当我对此进行测试时,返回的结果并不等于 400 错误代码。 我的代码: int? null
在我的使用 ssl 与服务器通信的 Windows Phone 应用程序中出现了一些问题。对于从服务器 HttpStatusCode.NotFound 收到的每个请求客户端。我该如何解决这个问题? 在
根据 IANA registry还有更多标准状态代码,例如 507 (InsufficientStorage)。我正在编写一个 webdav 服务器库,并且在我的类中使用 HttpStatusCode
我们已经使用 WCF 编写了一个 Restful 服务,现在正在尝试编写 FitNesse 测试来验证该服务的行为。 一个测试是如果找不到资源则返回 404 状态码。 问题是我们找不到一种机制来使用允
如果我将客户端连接到的服务指向一个错误的 URL,为什么我得到的 HttpStatusCode 为 0。 我的 statusCodeAsInt 显示为 0。为什么它没有显示为 404 并被处理? IR
我想知道是否有更简单的方法(更好的方法)来检查 500 状态代码? 我能想到的唯一方法是: var statusCodes = new List() { HttpStatusCode.BadGat
我正在使用 .NET MVC。 当我回来时new HttpResult(HttpStatusCode.NoContent);创建的对象具有状态代码 = 200: {ServiceStack.Commo
我正在尝试使用 ServiceStack.Razor 运行一个最小的应用程序,但我在运行 CustomHttpHandler 时遇到了问题。我已按照说明进行操作 here和 here ,但它无法正常工
这个问题在这里已经有了答案: Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWe
我正在使用 ASP.NET MVC 4,我需要返回状态代码 420,这在 HttpStatusCode 枚举值中找不到。 通常这很简单,你可以这样做: return Request.CreateRes
我正在编码 C# WebApi 2 webservice,我有一个关于返回 HttpStatusCode 的问题来自 IQueryable网络服务功能。 如果网络服务函数返回单个对象,则可以轻松使用以
我正在设置 ProducesResponseType,以便用 Swagger 记录它。 如果响应成功(OK => 200),那么它会生成一个 IEnumerable。 [ProducesRespons
根据以下网站,您可以将异常参数传递给 HttpResponseMessage.CreateErrorResponse 方法 ( https://msdn.microsoft.com/en-us/lib
我是一名优秀的程序员,十分优秀!