[英]ASP.NET core FromQuery getting invalid parameters with dot sign inside
I am trying to send some parameters with GET request to the controller in query string.我正在尝试在查询字符串中将一些带有 GET 请求的参数发送到控制器。 I am getting all parameters that are inside query string to one
Dictionary<string, string>
using [FromQuery]
.我正在使用
[FromQuery]
将查询字符串中的所有参数添加到一个Dictionary<string, string>
中。 It is working well until one of the parameter name contains dot ( .
) sign.它运行良好,直到其中一个参数名称包含点 (
.
) 符号。 I checked Request.Query
object and it looks that it's parsed well but my Dictionary object get this one, exact item wrong.我检查了
Request.Query
对象,它看起来解析得很好,但是我的 Dictionary 对象得到了这个,确切的项目是错误的。 So it looks like bug in [FromQuery]
binder or i am doing something wrong.所以它看起来像
[FromQuery]
活页夹中的错误,或者我做错了什么。 Code with debbuged values of Request.Query
and mine parameters
below:带有
Request.Query
调试值的代码和以下我的parameters
:
This is how the query string that is sent looks like: ?query=&InspectionType=Safety&ItemType=InspectionPoint&RecordParentGUID=9275bee2-0a2d-461c-8835-51880e76f035&parent.ResultClassCode=parent.ResultClassCode
发送的查询字符串如下所示:
?query=&InspectionType=Safety&ItemType=InspectionPoint&RecordParentGUID=9275bee2-0a2d-461c-8835-51880e76f035&parent.ResultClassCode=parent.ResultClassCode
UPDATE: Got answer from Eilon Lipton working in .net developers team, in short - this is by design.更新:简而言之,从在 .net 开发人员团队工作的 Eilon Lipton 得到答案 - 这是设计使然。 Dot sign
.
点号
.
and [
, ]
are special ones used for denoting properties and indexers.和
[
, ]
是用于表示属性和索引器的特殊字符。 Full answer available here: https://github.com/aspnet/Mvc/issues/6746此处提供完整答案: https ://github.com/aspnet/Mvc/issues/6746
The .这 。 (dot), [ and ] characters are used everywhere internally by ASP.NET MVC Core as path separators in the model binding process and there is (AFAIK) no way around that.
(点)、[ 和 ] 字符在 ASP.NET MVC Core 内部随处使用作为模型绑定过程中的路径分隔符,并且(AFAIK)没有办法解决这个问题。
If you want to access the raw values from the query string, Request.Query
is by far the simplest solution.如果您想从查询字符串中访问原始值,
Request.Query
是迄今为止最简单的解决方案。
To turn it into a pure Dictionary<string, string>
:把它变成一个纯
Dictionary<string, string>
:
Request.Query.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Can you use a partial view to flatten parent.ResultClassCode to ResultClassCode?您可以使用局部视图将 parent.ResultClassCode 展平为 ResultClassCode 吗?
The partial could look like this部分可能看起来像这样
@model parent
<div class="form-group">
<label asp-for="ResultClassCode"></label><span asp-validation-for="ResultClassCode" class="text-danger"></span>
<input asp-for="ResultClassCode" class="form-control">
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.