你怎么扔的Htt presponseException在ASP 5(vnext) [英] How do you throw HttpResponseException in ASP 5 (vnext)

查看:76
本文介绍了你怎么扔的Htt presponseException在ASP 5(vnext)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在ASP 5.我想返回错误请求code异常,如果传递给服务的参数不正确的API控制器。在的WebAPI的当前版本中,我会做:

I'm writing an api controller in ASP 5. I want to return a bad request code exception if the parameters passed to the service are incorrect. In the current version of webapi I would do:

throw new HttpResponseException(HttpStatusCode.BadRequest);

的Htt presponseException 的System.Web 的一部分,它已经从ASP 5删除,因此,我不能再实例化。

However HttpResponseException is part of System.Web, which has been removed from ASP 5 and thus I cannot instantiate it anymore.

什么是vNext这样做的正确方法?

What is the proper way to do this in vNext?

推荐答案

要回答你的问题,你可以使用的 WebApiCompatibilityShim 哪些端口的Htt presponseException (和许多其他功能)迈进MVC 6.(感谢DWright的链接<一个href=\"http://www.strathweb.com/2015/01/migrating-asp-net-web-api-mvc-6-exploring-web-api-compatibility-shim/\"相对=nofollow>这篇文章。)

To answer your question, you can use the WebApiCompatibilityShim which ports HttpResponseException (and many other features) forward into MVC 6. (thanks to DWright for the link to that article.)

好像要做到这一点是从你的方法返回一个IActionResponse,然后再调用MVC-6路 HttpBadRequest()这是基本<$上的方法C $ C> Microsoft.AspNet.Mvc.Controller 类,它返回一个 BadRequestResult ,我相信是获得400进入状态的最佳方式响应

It seems like the MVC-6 way to do it is to return an IActionResponse from your method and then call HttpBadRequest() which is a method on the base Microsoft.AspNet.Mvc.Controller class that returns an BadRequestResult, which I believe is the best way to get a 400 status into the response.

控制器类有其他响应codeS以及方法 - 包括 HttpNotFound()方法这会导致404要返回。

The Controller class has methods for other response codes as well - including the HttpNotFound() method which causes a 404 to be returned.

另请参阅: HTTP ://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6

这篇关于你怎么扔的Htt presponseException在ASP 5(vnext)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆