.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 中国大陆许可协议进行许可。

posted @   暴躁老砚  阅读(2212)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.