ASP.Net MVC - HTTP 状态代码(即 303、401、404 等)

发布于 2024-08-16 11:53:23 字数 281 浏览 6 评论 0原文

在 ASP.Net MVC 中,是否可以使用 RedirectRedirectToAction 来调用 303 错误等?

我正在阅读一本名为“ASP.NET MVC 1.0 Website Programming”的书,源代码正在进行诸如 return this.Redirect(303, FormsAuthentication.DefaultUrl); 之类的调用,但此调用仅起作用对于外部库,如果可能的话,我希望拥有相同的功能而无需附加组件。

In ASP.Net MVC is it possible to use either Redirect or RedirectToAction to call for example a 303 error?

I am working my way through a book titled "ASP.NET MVC 1.0 Website Programming" and the source is making calls such as return this.Redirect(303, FormsAuthentication.DefaultUrl); but this call only functions with an external library, I would like to have the same functionality sans the add-on if possible.

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

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

发布评论

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

评论(4

旧时模样 2024-08-23 11:53:23

您可以创建自定义 ActionResults 来模仿您想要的任何 http 响应代码。通过返回这些操作结果,您可以轻松执行 303。

我发现 这篇快速文章,您应该能够轻松理解。

You can create custom ActionResults that mimic any http response code you'd like. By returning those action results, you can easily perform a 303.

I found this quick write-up that you should be able to follow easily.

澉约 2024-08-23 11:53:23

这是我根据当前答案中给出的建议和反编译的 System.Web.Mvc.RedirectResult 代码得出的结论:

public class SeeOtherRedirectResult : ActionResult
{
    public string Url { get; private set; }
    public HttpStatusCode StatusCode { get; private set; }

    public SeeOtherRedirectResult(string url, HttpStatusCode statusCode)
    {
        if (String.IsNullOrEmpty(url))
        {
            throw new ArgumentException("URL can't be null or empty");
        }
        if ((int) statusCode < 300 || (int) statusCode > 399)
        {
            throw new ArgumentOutOfRangeException("statusCode", 
                        "Redirection status code must be in the 3xx range");
        }
        Url = url;
        StatusCode = statusCode;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (context.IsChildAction)
        {
            throw new InvalidOperationException("Cannot redirect in child action");
        }

        context.Controller.TempData.Keep();
        context.HttpContext.Response.StatusCode = (int) StatusCode;
        context.HttpContext.Response.RedirectLocation =
                     UrlHelper.GenerateContentUrl(Url, context.HttpContext);
    }
}

Here's what I came up with based on the advice given in the current answers and the decompiled System.Web.Mvc.RedirectResult code:

public class SeeOtherRedirectResult : ActionResult
{
    public string Url { get; private set; }
    public HttpStatusCode StatusCode { get; private set; }

    public SeeOtherRedirectResult(string url, HttpStatusCode statusCode)
    {
        if (String.IsNullOrEmpty(url))
        {
            throw new ArgumentException("URL can't be null or empty");
        }
        if ((int) statusCode < 300 || (int) statusCode > 399)
        {
            throw new ArgumentOutOfRangeException("statusCode", 
                        "Redirection status code must be in the 3xx range");
        }
        Url = url;
        StatusCode = statusCode;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (context.IsChildAction)
        {
            throw new InvalidOperationException("Cannot redirect in child action");
        }

        context.Controller.TempData.Keep();
        context.HttpContext.Response.StatusCode = (int) StatusCode;
        context.HttpContext.Response.RedirectLocation =
                     UrlHelper.GenerateContentUrl(Url, context.HttpContext);
    }
}
多彩岁月 2024-08-23 11:53:23

关于此的两种方法-

  Response.Redirect(url, false); //status at this point is 302
  Response.StatusCode = 303;

-或-

  Response.RedirectLocation = url;
  Response.StatusCode = 303;

请注意,在第一个重定向中, false 参数避免了 redirect(url) 通常引发的 threadAbort 异常。这是使用这两种方法之一的充分理由。

2 ways about this-

  Response.Redirect(url, false); //status at this point is 302
  Response.StatusCode = 303;

-or-

  Response.RedirectLocation = url;
  Response.StatusCode = 303;

Note that in the first redirect, that the false parameter avoids the threadAbort exception that the redirect(url) normally throws. This is one good reason to use one of these 2 methods.

陪我终i 2024-08-23 11:53:23

您还可以使用自定义 ActionFilter 来完成它,就像我提到的 此处。我个人更喜欢 ActionFilter 而不是 ActionResult。

You could also accomplish it with a custom ActionFilter like I mention here. I for one like the ActionFilter a bit more the the ActionResult.

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