2010-12-22 21 views
21

içinde istemciye http 204 "içerik yok" dönüş Bir ASP.NET MVC 2 uygulamasında bir post işlemine 204 No Content yanıtı dönmek istiyorum. Geçerli denetleyici yöntemimin geçersiz bir dönüş türü vardır, ancak bu, istemciye bir yanıtı 200 OK olarak, Content-Length üstbilgisi 0 olarak ayarlayarak geri gönderir. Yanıtı 204'e nasıl yapabilirim?ASP.NET MVC2

[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 
} 

cevap

29

MVC3'te HttpStatusCodeResult class bulunmaktadır. Bir MVC2 uygulaması için kendi rulo olabilir:

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; 
    } 
} 

Çok sevdiği denetleyici yöntemini değiştirmek olurdu:

[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); 
} 
İlgili konular