IIS 8.5下AJAX GET超196B返回HTML致404问题解决
.NET Core应用部署到IIS 8.5后,GET请求传长文本出现404错误的解决方法
问题背景
运行在Windows Server 2012(IIS 8.5)上的.NET Core笔记应用,本地调试时通过AJAX GET请求更新数据库nvarchar(max)类型文本字段一切正常,但部署到生产环境后,当文本描述较长时,Firefox控制台会出现404错误,更新功能静默失败。
排查发现存在临界长度:成功保存的JSON格式文本传输大小为196B,仅添加一个字符后就会失败,此时返回1.42KB的HTML内容。
原AJAX代码:
$.ajax({ type: "GET", contentType: "application/json; charset=utf-8", url: '@Url.Action("UpdateMeetingItem", "Meetings")', data: { "id": id, "DateOpen": DateOpen, "DateClosed": DateClosed, "Responsibility": Responsibility, "descID": d_ID, "Description": Description }, dataType: "json", success: function (data) { if (data.result == true) { //do stuff here } else { alert("There is some error updating the Meeting Item."); } } })
成功保存的文本示例:
This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in the production server application. On the development side I don't see any issues. This is just a bunch of sample text to attempt to reproduce the problem I see in
问题原因
核心原因是GET请求的URL长度限制:GET请求会把所有参数拼接在URL末尾,而IIS 8.5默认对GET请求的URL长度有上限(默认约4096字节),当文本内容过长导致总URL长度超过这个限制时,IIS会直接返回404错误。本地调试时使用的开发服务器(如Kestrel)URL限制更宽松,因此不会触发该问题。
解决方法
推荐方案:改用POST请求传输数据
这是已验证有效的方案,同时符合HTTP规范——POST请求的参数放在请求体中,不受URL长度限制,更适合传输包含长文本的更新数据。
调整步骤:
- 修改AJAX请求为POST,并用
JSON.stringify()格式化请求数据:
$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: '@Url.Action("UpdateMeetingItem", "Meetings")', data: JSON.stringify({ "id": id, "DateOpen": DateOpen, "DateClosed": DateClosed, "Responsibility": Responsibility, "descID": d_ID, "Description": Description }), dataType: "json", success: function (data) { if (data.result == true) { //do stuff here } else { alert("There is some error updating the Meeting Item."); } } })
- 控制器方法添加
[FromBody]特性接收请求体数据:
public JsonResult UpdateMeetingItem([FromBody]MeetingPostModel m) { // 更新数据库等业务逻辑 }
不推荐方案:修改IIS的URL长度限制
虽然可以通过修改IIS配置放宽GET请求的URL长度限制,但不建议这么做:
- 不符合HTTP设计规范:GET请求不应用于传输大量数据或修改服务器状态(更新数据库属于状态修改,应使用POST/PUT等方法)。
- 存在安全风险:过长的URL可能被代理服务器、防火墙拦截,也易暴露敏感数据。
若确需调整,可在站点web.config中添加以下配置(仅作参考):
<system.webServer> <security> <requestFiltering> <requestLimits maxUrl="10000" maxQueryString="10000" /> </requestFiltering> </security> </system.webServer>
内容的提问来源于stack exchange,提问作者Paul Gibson
相关产品推荐
相关产品推荐

