MVC控制器返回错误请求? [英] MVC Controller return a bad request?

查看:158
本文介绍了MVC控制器返回错误请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从MVC控制器返回包含内容的错误请求?我能够执行此操作的唯一方法是throw HttpException,但是在这里我无法设置任何内容.尝试了这种方法,但出于某些奇怪的原因,我总是能接受.可以这样做吗?

I was wondering if it was possible to return a bad request with content from an MVC Controller? The only way I have been able to do this is to throw HttpException however here I can't set any content. Tried this approach to but for some odd reason I am always getting an OK back. Is it possible to do this?

public class SomeController : Controller
{
    [HttpPost]
    public async Task<HttpResponseMessage> Foo()
    {
        var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
        response.Content = new StringContent("Naughty");

        return response;    
    }
}

推荐答案

return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "naughty");

这篇关于MVC控制器返回错误请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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