Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net core 3 webapi:名称中带有连字符的路由或绑定参数(查询或发布)_C#_Asp.net Core - Fatal编程技术网

C# asp.net core 3 webapi:名称中带有连字符的路由或绑定参数(查询或发布)

C# asp.net core 3 webapi:名称中带有连字符的路由或绑定参数(查询或发布),c#,asp.net-core,C#,Asp.net Core,我希望一个动作签名像 [HttpPost] public string Post(string thirdPartySpecifiedParameter) 将自动绑定带连字符的查询参数 controller?第三方指定的参数=值 或JSON发布的值 { "third-party-specified-parameter":"value" } 并将其分配给第三方指定的参数,但它没有。路由文档处理URL路径中的连字符映射示例,但不处理绑定参数和字段名,这些参数和字段名对C#无效 将传入的连字符名

我希望一个动作签名像

[HttpPost]
public string Post(string thirdPartySpecifiedParameter)
将自动绑定带连字符的查询参数

controller?第三方指定的参数=值
或JSON发布的值

{ "third-party-specified-parameter":"value" }
并将其分配给第三方指定的参数,但它没有。路由文档处理URL路径中的连字符映射示例,但不处理绑定参数和字段名,这些参数和字段名对C#无效


将传入的连字符名称绑定到匹配的C参数的最简单方法是什么?

首先,问题是绑定而不是路由,最简单的可用解决方案是使用.Net提供的
BindingAttribute
。限制——不是新的限制——是查询参数vs Json Body vs Form post需要不同的BindingAttribute

[HttpPost]
public string Post(
    [FromBody]PostModel model,
    [FromQuery(Name="kebab-case-query-param")]string kebabCaseQueryParam)
[FromQuery(Name=“…”)]
处理查询字符串参数

对于Json帖子,您必须在方法签名中使用
[FromBody]
属性,定义一个模型,并在要绑定的属性上放置
属性
Newtonsoft.Json
或new-in-core-3
System.Text.Json
使用稍微不同的
属性
名称:

public class PostModel
{
    //This one if you are using Newtonsoft.Json
    [JsonProperty(PropertyName = "kebab-case-json-field")]

    //This one of you are using the new System.Text.Json.Serialization
    [JsonPropertyName("kebab-case-json-field")]

    public string kebabCaseProperty { get; set; }
}
回到
Startup.cs
,要使用Newtonsoft,还需要
AddMvc()
,而对于新的
System.Text.Json
,则不需要。大概是这样的:

    public void ConfigureServices(IServiceCollection services)
    {
        //if using NewtonSoft
        services.AddMvc().AddNewtonsoftJson();
    
        //if using System.Text.Json
        //dotnet new webapi for netcore3 generates this code:
        services.AddControllers();
        
    }

要在NetCore3下以这种方式使用Newtonsoft Json,您需要依赖nuget软件包
Microsoft.AspNetCore.Mvc.NewtonsoftJson

检查这一点,很可能您使用的是新的默认
System.Text.Json
,它无法处理这一问题,只是在AspNetCore 3.1.3上使用了
dotnet新webapi
模板,模板是最新的,我添加的唯一代码是这里描述的一个方法。注意,我编辑了Q,因为我意识到我真正想问的是绑定而不是路由NKOSI,是的,我明白你的观点——对于json发布,这就是问题所在。请随意写一个答案,否则我会写一个来涵盖我的两个用例