c# - FromUrl 模型的自定义属性名称
问题描述
我有一个用于绑定 QueryString 的模型,它遵循 c# 的命名对话,但 QueryString 处于不同的命名对话中。如何为分配给 vis 的模型属性提供自定义属性名称FromUrl
?
// Will NOT work
public class FormatDatabaseRequest
{
[JsonProperty("_type")]
public string Type { get; set; }
[JsonProperty(Name = "awef_flag")]
public string AwefFlag { get; set; }
}
// Controller.cs
[HttpPost]
public async Task<HttpResponseMessage> FormatDatabaseAsync([FromUri] FormatDatabaseRequest request) {}
// Sample URL (QueryString MUST be named _type and awef_flag)
// https://localhost:43521/myControllerName?_type=asdfa&awef_flag=asdf
解决方案
如果你想像这样从 URL 中获取字段,我建议使用[FromQuery]
属性,如下所示:
public async Task<HttpResponseMessage> Get([FromQuery] FormatDatabaseRequest data)
那么,这样的网址
https://localhost:43521/myControllerName?type=asdfa&awefflag=asdf
将被正确解析为您的对象:)
推荐阅读
- r - R Sub函数替换有空格的模式
- wordpress - WordPress 单一 IP 允许 wp-admin 访问
- java - 在 Java 中向 Concordance 函数添加频率计数
- node.js - 有没有一种优雅的方式来呈现两组猫鼬文档?
- javascript - 有没有办法在javascript中做类似 if (Variable == 1 or Variable == 2 or Variable == 3) 的事情?
- python - 基于移位值/递归算法的系列计算
- bash - 如果“>”在命令之后,为什么 bash 重定向会在命令启动之前*发生?
- google-cloud-platform - GCP 实例:Ipython Notebook 下载到本地系统
- html - 在带有 Angular 条件类的 HTML 表格上设置边框在 Safari 上不起作用
- syntax - SAS 关于连接运算符的一些问题 ||