我有一个模型类,我想将查询字符串绑定到我的ASP.NET MVC Core(RC2)应用程序中。
我需要在查询字符串键中支持下划线以确认OAuth规范,但我想在我的应用程序中使用标题案例属性名称。
我的模型类看起来像这样:
class OauthParameters
{
public string ClientId {get; set;}
public string ResponseType {get; set;}
public string RedirectUri {get; set;}
}
所以我想将client_id
,response_type
和redirect_uri
之类的查询字符串绑定到它。
ASP.NET MVC Core是否有办法自动或通过属性注释来实现?
我已经阅读了一些关于编写自定义模型绑定器的文章,但这些文章似乎(1)对于我想要实现的内容而言过于复杂,并且(2)是为RC1或更早版本而编写的。一些语法已经改变。
提前致谢。
答案 0 :(得分:32)
您可以在此处使用FromQuery
属性的Name
媒体资源。
示例:
public class OauthParameters
{
[FromQuery(Name = "client_id")]
public string ClientId { get; set; }
[FromQuery(Name = "response_type")]
public string ResponseType { get; set; }
[FromQuery(Name = "redirect_uri")]
public string RedirectUri { get; set; }
}
答案 1 :(得分:-3)
.net core 2.1和2.2的解决方案
或者没有属性,您可以做类似我想更干净的事情(当然,如果模型属性与查询参数相同)。
同时我在.net core 2.1和2.2中使用它
public async Task<IActionResult> Get([FromQuery]ReportQueryModel queryModel)
{
}