.NET Core 获取url中请求参数值(QueryString)
1、通过方法参数获取
可以[FromQuery]
用来将特定模型绑定到参数:
[HttpGet] public IActionResult Get([FromQuery(Name = "appid")] string appid) { Session result = DispatchHelper.GetSession(appid); if (result != null) return new JsonResult(new { openid = result.openid, session_key = result.session_key, unionid = result.unionid }); return new NoContentResult(); }
2、通过HttpContext.Request.Query获取
[HttpGet] public IActionResult GetPage() { string appid = HttpContext.Request.Query["appid"].ToString(); Session result = DispatchHelper.GetSession(appid); if (result != null) return new JsonResult(new { openid = result.openid, session_key = result.session_key, unionid = result.unionid }); return new NoContentResult(); }
3、通过model获取
通过model
中指定[FromQuery]
参数的属性来获取Url中的参数。
[HttpGet] public IActionResult GetPage(ApiModel model) { Session result = DispatchHelper.GetSession(model); if (result != null) return new JsonResult(new { openid = result.openid, session_key = result.session_key, unionid = result.unionid }); return new NoContentResult(); }
public class ApiModel { [FromRoute] public int Id { get; set; } [FromQuery] public string Url { get; set; } [FromQuery] public int? PageId { get; set; } }
本文作者:暴躁老砚
本文链接:https://www.cnblogs.com/Yan3399/p/16033638.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步