.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; }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM