在ASP.NET MVC中按优先级返回表单,查询字符串,Cookie值
时间:2020-03-06 14:52:37 来源:igfitidea点击:
我想知道为什么从用户请求中获取值时首选查询字符串。在哪里?
1)System.Web.Mvc.DefaultModelBinder的代码如下所示(仅一部分):
HttpRequestBase request = controllerContext.HttpContext.Request; if (request != null) { if (request.QueryString != null) { values = request.QueryString.GetValues(modelName); attemptedValue = request.QueryString[modelName]; } if ((values == null) && (request.Form != null)) { invariantCulture = CultureInfo.CurrentCulture; values = request.Form.GetValues(modelName); attemptedValue = request.Form[modelName]; } }
2)如果我在控制器中具有此签名的方法:
public ActionResult Save(int? x, string y) {...
参数(x,y)绑定到查询字符串而不是表单的值。
我希望Request.From的值比Request.QueryString的值具有更高的优先级。
编辑:我看到第二种情况是由第一种情况(DefaultModelBinder)引起的,对吗?
背后的动机是什么?
解决方案
一致性。
自原始ASP模型以来,查询字符串一直是默认字符串。如果要获取数据,那么如果查询字符串上也有相同的名称,则始终需要从那里显式地获取值。