在Asp.Net Core中使用POST方法重定向到URL [英] Redirect to URL with POST method in Asp.Net Core

查看:763
本文介绍了在Asp.Net Core中使用POST方法重定向到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的网址重写器:

I have simple url rewriter:

    private static void RedirectToAPI(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        if (request.Path.Value.StartsWith("/apiendpoint", StringComparison.OrdinalIgnoreCase))
        {           
            var json = JsonConvert.SerializeObject(request.Path.Value
                .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                .Skip(1));
            var response = context.HttpContext.Response;

            response.Headers[HeaderNames.Location] = $"/custom";
            response.StatusCode = StatusCodes.Status301MovedPermanently;
            context.Result = RuleResult.EndResponse;
            using (var bodyWriter = new StreamWriter(response.Body))
            {
                bodyWriter.Write(json);
                bodyWriter.Flush();
            }
        }
    }

问题是,当重定向到/custom url时,请求具有GET方法,而该方法需要POST.

The problem is, when it redirects to /custom url, request has method GET, while this method require POST.

例如,将GET请求发送到url /apiendpoint/first/second/third ,然后重写器响应重定向,因此,以下请求必须使用POST方法,但现在是GET

For example, send GET request to url /apiendpoint/first/second/third, then rewriter responds to redirect, accordingly, the following request must be with method POST, but now, it is GET.

在URL重写器响应之后,如何更改请求方法?

How can I change method of request, which is after url rewriter response?

推荐答案

啊,刚才检查了注释.如果初始请求是GET,则此方法也将不起作用,并且您将无法告诉浏览器进行POST.并非没有返回使用JavaScript自动执行表单的视图.

Ah, just now checked the comments. If the initial request is a GET, then this won't work either and you can't tell the browser to POST. Not without returning a view that auto-executes a form with JavaScript.

您需要返回308,而不是301.

You need to return a 308, not a 301.

这是更改后的代码:

private static void RedirectToAPI(RewriteContext context)
{
    var request = context.HttpContext.Request;
    if (request.Path.Value.StartsWith("/apiendpoint", StringComparison.OrdinalIgnoreCase))
    {           
        var json = JsonConvert.SerializeObject(request.Path.Value
            .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
            .Skip(1));
        var response = context.HttpContext.Response;

        response.Headers[HeaderNames.Location] = $"/custom";
        response.StatusCode = 308;
        context.Result = RuleResult.EndResponse;
        using (var bodyWriter = new StreamWriter(response.Body))
        {
            bodyWriter.Write(json);
            bodyWriter.Flush();
        }
    }
}

308是永久重定向,要求浏览器保留该方法. https://httpstatuses.com/308

308 is a permanent redirect that requires the browser to preserve the method. https://httpstatuses.com/308

临时版本是307.

在MVC Core 2.0.0-preview1中提供了用于这些重定向的便捷方法.

Convenience methods for these redirects are available in MVC Core 2.0.0-preview1.

这篇关于在Asp.Net Core中使用POST方法重定向到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆