.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