MVC2 POST下返回不同的响应代码

发布于 2024-11-02 20:48:16 字数 1292 浏览 0 评论 0原文

我正在使用 MVC2 开发 REST API。

在 Action 方法中处理 POST 请求时,应该使用值填充各种自定义响应标头,并设置特定的 HTTP 响应代码。

由于这是一个 POST 请求,我将返回一个 EmptyResult。

当我调用该网站时,我可以看到自定义标头正在填充,但响应代码(我使用 Response.StatusCode = ... 设置的)被忽略,并且我总是得到 200。

我的技术主管建议使用 HTTPException 来获取响应代码,但我认为这是错误的方法。无论如何,我已经尝试过了,正如我怀疑的那样,响应代码已写入,但其他所有内容(我的自定义响应标头)都搞砸了。

在 StackOverflow 上的类似线程之后,我尝试编写自己的 ActionResult 子类来处理这个问题,但同样 - 我只得到了 200 秒。

对于那些感兴趣的人来说,该课程在这里:

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

有谁知道能够指定响应代码并返回我的自定义标头的正确方法是什么? 顺便说一句,这似乎只是 POST 请求 - GET 请求似乎工作正常......

干杯,

马丁。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-11-09 20:48:16

如果它与 get 一起使用,您可以在处理 post 后重定向到另一个操作,在那里您可以设置 repspose 标头

if it works with get you can redirect to another action after handling post and there you can set the repspose header

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文