ASP.NET MVC 忽略内容长度?
我在 ASP.NET MVC 中遇到了一些丢失发布数据的问题,这促使我研究 ASP.NET MVC 如何处理无效的内容长度。我原以为 MVC.NET 应该忽略内容长度无效的帖子,但事实似乎并非如此。
作为示例,尝试创建一个新的 ASP.NET MVC 2 Web 应用程序并将此操作添加到 HomeController:
public ActionResult Test(int userID, string text)
{
return Content("UserID = " + userID + " Text = " + text);
}
尝试创建一个发布到上述操作的简单表单,运行 fiddler 并(使用“Request Builder”)修改原始数据,以便某些表单数据丢失(例如删除文本参数)。在执行请求之前,请记住取消选中“请求生成器”选项下的“修复内容长度标头”复选框,然后在上面的代码上设置断点并执行自定义 http 请求。
我发现该请求比正常情况要长得多(30 秒左右),但令我惊讶的是,控制器操作仍然处理该请求。有谁知道这是否是预期的行为,如果是,您会建议采取什么措施来防止无效的内容长度?
I've been having some problems with missing post data in ASP.NET MVC which has lead me to investigate how ASP.NET MVC deals with invalid content lengths. I had presumed that a post with a invalid content length should be ignored by MVC.NET but this doesn't seem to be the case.
As an example, try creating a new ASP.NET MVC 2 web application and add this action to the HomeController:
public ActionResult Test(int userID, string text)
{
return Content("UserID = " + userID + " Text = " + text);
}
Try creating a simple form that posts to the above action, run fiddler and (using "Request Builder") modify the raw data so that some of the form data is missing (e.g. remove the text parameter). Before executing the request, remember to un-tick the "Fix Content-Length header" checkbox under the Request Builder options then set a break point on the code above and execute the custom http request.
I find that the request takes a lot longer than normal (30 seconds or so) but to my amazement is still processed by the controllers action. Does anyone know if this is expected behavior and, if so, what would you recommend to safeguard against invalid content-lengths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET 不会忽略
Content-Length
请求标头。考虑以下控制器操作作为示例,它只是回显foo
参数:现在让我们向它发出一个有效的 POST 请求:
正如预期的那样,这将打印响应 HTTP 标头(这并不重要)并在我们有
foobar
主体。现在尝试减少请求的Content-Length
标头:这会在响应正文中返回单个
f
。正如您所看到的,无效的 HTTP 请求可能会导致参数解析不正确。ASP.NET does not ignore the
Content-Length
request header. Consider the following controller action as an example which simply echoes back thefoo
parameter:Now let's make a valid POST request to it:
As expected this prints the response HTTP headers (which are not important) and in the body we have
foobar
. Now try reducing theContent-Length
header of the request:Which returns a single
f
in the response body. So as you can see an invalid HTTP request could lead to incorrect parsing of the parameters.