回发总是返回状态代码 301 Moved Permanently

1 asp.net-mvc redirect http visual-studio iis-express

任何类型的回发、Ajax 或标准 M​​VC 表单提交,都返回状态代码 301。这似乎与动作或控制器是什么没有区别。将浏览器从 Chrome 更改为 Firefox 没有帮助。

<script>
$('.zzz').click(function (e)
{
    e.preventDefault();
    e.stopPropagation();

    $.ajax({
        url: '/XXX/yyy',
        data: { test: "hello" },
        type: "post",
        success: function () { alert("success"); },
        error: function () { alert("error"); }
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

控制器:

public class XXXController : AsyncController
{
    [HttpPost]
    public ActionResult YYY()
    {
        return null;     // ====== NEVER REACHES HERE
    }
}
Run Code Online (Sandbox Code Playgroud)

标题

Request URL:http://localhost:47038/xxx/yyy
Request Method:POST
Status Code:301 Moved Permanently
Remote Address:[::1]:47038
Referrer Policy:no-referrer-when-downgrade

Response Headers
=================
Access-Control-Allow-Origin:*
Content-Length:154
Content-Type:text/html; charset=UTF-8
Date:Tue, 21 Nov 2017 16:02:23 GMT
Location:http://localhost:47038/xxx/yyy/
Server:Microsoft-IIS/10.0
X-Frame-Options:SAMEORIGIN
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcdmF1bHRccHJpbnRlcnBpeG12Y3VpXFByaW50ZXJQaXhNdmNVSVxYWFhcWVlZ?=

Request Headers
================
view source
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-GB,en-US;q=0.9,en;q=0.8
Connection:keep-alive
Content-Length:10
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ASP.NET_SessionId=rargvavdg0reeuhqvqkznsaj; MachineToken=a2fec363-6318-4ec3-8d2c-0eee116fc778; __RequestVerificationToken=80gj5joNWUpBjgjOsxkV0SkDwhrX3fNbzYTZrTaUGpJXlIEY7nyguehSDpz525JKyNfjlI5Two-poQs1dC2jw0kWnpvnK74iz4X3KV5MtSI1
Host:localhost:47038
Origin:http://localhost:47038
Referer:http://localhost:47038/xxx/Index/?product=puzzle
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
X-Requested-With:XMLHttpRequest

Form Data
==========
test:hello
Run Code Online (Sandbox Code Playgroud)

GET 请求工作正常,只是 POST 被重定向。

为什么会返回 301 "Moved Permanently",而不是执行 Action 方法?

小智 6

解决方案是在回发 URL 的末尾放置一个正斜杠:

url: '/XXX/yyy'   ==> 301 Moved Permanently
url: '/XXX/yyy/'  ==> 200 OK
Run Code Online (Sandbox Code Playgroud)

很尴尬的浪费一天的工作。