asp.net-mvc 返回空的 ActionResult
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9196773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning Empty ActionResult
提问by Maxim V. Pavlov
I may encounter situations, when I need to just return bad request result.
我可能会遇到一些情况,当我只需要返回错误的请求结果时。
For example, there is a call to MVC 3 site's controllers action, but the required parameter is missing in a request uri.
例如,有一个对 MVC 3 站点的控制器操作的调用,但请求 uri 中缺少必需的参数。
What do I return in response. I know I can do this:
我返回什么作为回应。我知道我可以这样做:
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content(string.Empty);
Is this the correct way for the above described situation?
这是上述情况的正确方法吗?
回答by Sergey Kudriavtsev
Your solution will work OK, but more clear way will be using HttpStatusCodeResultclass, like this:
您的解决方案可以正常工作,但更清晰的方法是使用HttpStatusCodeResult类,如下所示:
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

