I am developing a REST API using MVC2.
When handling a POST request in an Action method, am supposed to populate various custom Response headers with values, and also set specific HTTP response codes.
Since this is a POST request, I am returning an EmptyResult.
When I call against the site, I can see that the custom headers are getting populated, but the response code (which I have set using Response.StatusCode = ...) is ignored, and I always get 200.
My tech lead has suggested using a HTTPException to get the Response code, but I regard this as the wrong apprach. I've tried it anyway, and as I suspected, the response code is write, but everything else (my custom Response Headers) is screwed up.
Following a similar thread on StackOverflow, I have tried to write my own subclass of ActionResult to handle this, but again - I just get 200s back.
The class, for those interested, is here:
Public Class HttpStatusCodeActionResult : Inherits ActionResult
#Region "Data Members"
Private mintStatusCode As Integer = 0
Private mstrStatusDescription As String = ""
#End Region
Public Sub New(ByVal vintStatusCode As Integer, ByVal vstrStatusDescription As String)
mintStatusCode = vintStatusCode
mstrStatusDescription = vstrStatusDescription
End Sub
Public Overrides Sub ExecuteResult(ByVal context As System.Web.Mvc.ControllerContext)
context.HttpContext.Response.StatusCode = mintStatusCode
If mstrStatusDescription <> "" Then
context.HttpContext.Response.StatusDescription = mstrStatusDescription
End If
End Sub
End Class
Does anyone have any idea what the correct approach might be to being able to specify the response code AND return my custom headers? This only seems to be POST requests by the way - GET requests seem to work fine...
Cheers in anticipation,
Martin.
if it works with get you can redirect to another action after handling post and there you can set the repspose header