本文介绍了ASP.NET MVC 4,抛出HttpException VS回报的HTTPStatus codeResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发一个RESTful服务,我要回400所有不支持的URL。
I am developing a RESTful service and I want to return 400 for all unsupported URLs.
我的问题是的我应该选择方法1在方法2,反之亦然。
//method 1
public ActionResult Index()
{
//The url passed is unsupported
throw new HttpException(400, "Bad Request");
}
这一次似乎是更好的?
//method 2
public ActionResult Index()
{
//The url passed is unsupported
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request");
}
推荐答案
第二似乎更好,因为它不涉及其是在一个微成本相比于第一实施例的异常投掷。
The second seems better as it doesn't involve exception throwing which comes at a micro-cost compared to the first example.
这篇关于ASP.NET MVC 4,抛出HttpException VS回报的HTTPStatus codeResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!