C# “的Camelcase系列化;从路线&x201D;请求属性
在我的ASP.NET Core 2.1 MVC应用程序中,我想公开如下路由:C# “的Camelcase系列化;从路线&x201D;请求属性,c#,asp.net-core,asp.net-core-webapi,asp.net-core-2.1,C#,Asp.net Core,Asp.net Core Webapi,Asp.net Core 2.1,在我的ASP.NET Core 2.1 MVC应用程序中,我想公开如下路由: /address/v1/postcode/{postcode}/street/{street} [Route("address/v1")] [ApiController] public class StreetController : ControllerBase { [HttpGet("postcode/{postcode}/street/{street}")] public ActionResul
/address/v1/postcode/{postcode}/street/{street}
[Route("address/v1")]
[ApiController]
public class StreetController : ControllerBase
{
[HttpGet("postcode/{postcode}/street/{street}")]
public ActionResult<GetStreetDetailsResponse> GetStreetDetails([FromRoute] GetStreetDetailsRequest request)
{
throw new NotImplementedException();
}
}
public class GetStreetDetailsRequest
{
[Required]
[StringLength(4, MinimumLength = 4)]
[RegularExpression("^[\\d]+$")]
public string Postcode { get; set; }
[Required]
public string Street { get; set; }
}
public class GetStreetDetailsResponse
{
}
我对控制器的定义如下:
/address/v1/postcode/{postcode}/street/{street}
[Route("address/v1")]
[ApiController]
public class StreetController : ControllerBase
{
[HttpGet("postcode/{postcode}/street/{street}")]
public ActionResult<GetStreetDetailsResponse> GetStreetDetails([FromRoute] GetStreetDetailsRequest request)
{
throw new NotImplementedException();
}
}
public class GetStreetDetailsRequest
{
[Required]
[StringLength(4, MinimumLength = 4)]
[RegularExpression("^[\\d]+$")]
public string Postcode { get; set; }
[Required]
public string Street { get; set; }
}
public class GetStreetDetailsResponse
{
}
一切正常。然而,我对这个解决方案不满意,因为它没有遵循传统的C#命名标准
我尝试过用[DataMember(Name=“postcode”)]或[JsonProperty(“postcode”)]装饰属性,但这些似乎也被忽略了
作为记录,在我的Startup.ConfigureServices()方法中,我使用的是默认序列化程序,我知道它支持驼峰情况:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
有没有人找到一个解决方案,可以让我在请求对象属性名称中使用Pascal case来公开带有驼峰大小写属性的路由?好吧,从某种程度上说,你是正确的。这:
[HttpGet("postcode/{postcode}/street/{street}")]
表示您有一个邮政编码
和一个街道
属性,但您没有这些属性。如果希望默认绑定工作,则大小写必须完全匹配:
[HttpGet("postcode/{Postcode}/street/{Street}")]
谢谢,是的,我应该说我发现[HttpGet(“postcode/{postcode}/street/{street}”)]也起了作用,但这并没有给我在Swashback生成的swagger文档中想要的路线文档(驼峰案例)。