【发布时间】:2020-02-07 01:22:28
【问题描述】:
我正在开发一个 ASP.NET Core 2.1 应用程序。我需要通过 ajax 调用一个命名的页面处理程序。
该方法在调试/发布时可以正常工作,但一旦发布到 IIS 就会失败。
我尝试过的
- 我现在使用 @page "{handler?}" 而不是将处理程序指定为查询参数
- 我已确保请求 url 正确,并且表单包含“__RequestVerificationToken”
- 我已将 IIS 应用程序池 CLR 版本更改为“无托管代码”
- 我已将 web.config 修改为如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
</configuration>
代码:
这是处理程序方法,其他页面中也有类似的方法,但ajax请求没有找到它们(但同样,只有在部署到IIS后才会发生这种情况):
[HttpPost]
public IActionResult OnPostExampleHandler([FromForm]ExampleHandlerModel model)
{
// ...
return new JsonResult(new { data, message });
}
这是提交时调用的javascript代码:
function submitForm()
{
return $.ajax(
{
type: "POST",
url: "/relativeURL/ExampleHandler",
data: $('#ExampleHandlerForm').serialize(),
dataType: "json",
complete: function (res)
{
// do something on completion
}
});
}
最后这是 Html/Razor 代码,我正在使用“表单”TagHelper,它确保在表单被序列化时出现 AntiForgeryToken:
<form method="post" asp-page-handler="ExampleHandler" id="ExampleHandlerForm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@Model.Details.Description</h5>
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body form-row">
@* Input Fields *@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">@Lang.Operations.Close</button>
<button type="button" onclick="submitForm();" class="btn btn-primary">@Lang.Operations.Save</button>
</div>
</div>
</form>
错误信息
POST http://root:port/relativeURL/ExampleHandler 404 (Not Found)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
submitForm @ VM106:13
onclick @ 5:1
【问题讨论】:
-
404 是一个凭证问题。服务器可能需要 HTTPS(安全 SSL),而您不会执行 SSL。请参阅:codeproject.com/Articles/1000189/…
-
@jdweng 但是为什么它只会发生在 Ajax Post 请求中呢?我还为该项目禁用了 SSL 并删除了 app.UseHttpsRedirection();在启动配置中,因为在这个阶段我还不需要 https
-
您的应用程序是否托管在 IIS 下的虚拟应用程序上,如果是,则路径应包含该虚拟应用程序名称。
-
使用像wireshark或fiddler这样的嗅探器,并将第一个请求中的标头与工作和非工作进行比较。
-
@jdweng 标题似乎是相同的,除了
Accept-Encoding: gzip, deflate,在工作版本上是Accept-Encoding: gzip, deflate, br,但据我所知,这是与浏览器相关的,我无法更改通过 Ajax?
标签: c# ajax asp.net-core razor-pages iis-10