asp.net-mvc-2 – 在ASP.NET MVC2中向客户端返回http 204“无内容”

在ASP.net MVC 2应用程序中,我想要返回一个204无内容响应的后期操作。当前我的控制器方法一个无效的返回类型,但是它将一个响应发送回到客户端为200 OK,Content-Length头设置为0.如何使响应成为204?
[HttpPost]
public void DoSomething(string param)
{
    // do some operation with param

    // Now I wish to return a 204 no content response to the user 
    // instead of the 200 OK response
}

解决方法

在MVC3中有一个 HttpStatusCodeResult class.你可以自己滚动一个MVC2应用程序:
public class HttpStatusCodeResult : ActionResult
{
    private readonly int code;
    public HttpStatusCodeResult(int code)
    {
        this.code = code;
    }

    public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
    {
        context.HttpContext.Response.StatusCode = code;
    }
}

你必须像这样改变你的控制器方法

[HttpPost]
public ActionResult DoSomething(string param)
{
    // do some operation with param

    // Now I wish to return a 204 no content response to the user 
    // instead of the 200 OK response
    return new HttpStatusCodeResult(HttpStatusCode.NoContent);
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....