请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

汇总:ASP.NET Core中HttpContext获取传参数据,有哪些方式

[复制链接]
查看3041 | 回复1 | 2019-11-8 09:54:28 | 显示全部楼层 |阅读模式

一、原生方式:

1.POST(以ajax哀求为案例,教各人用法)

  1. $.ajax({
  2. type: "post",
  3. dataType: "json",
  4. cache: false,
  5. data: {
  6. method: "add"
  7. },
  8. url: "../demo/post",
  9. async: true,
  10. success: function (data) {
  11. if (data.isOK) {
  12. alert("成功");
  13. }
  14. else {
  15. alert(“失败”);
  16. }
  17. }
  18. });
复制代码

  1. IFormCollection form = HttpContext.Request.Form;
  2. string method = form["method"];
复制代码

2.GET(url传参为案例,教各人用法)

  1. 127.0.0.1/index/demo/get?num=1
复制代码

  1. IQueryCollection queryParameters = HttpContext.Request.Query;
  2. string num = queryParameters["num"];
复制代码

二、以对象的情势吸收参数(get/post通用):

  1. public class PageModel
  2. {
  3. public string TitleName { get; set; }//筛选标题
  4. public int CurrentPage { get; set; }//当前页
  5. public int NumCount { get; set; } //每页数量
  6. public long Id { get; set; } = 0;//默认id
  7. public string Token { get; set; } = "";//认证授权
  8. }
复制代码
  1. public IActionResult UserList(PageModel pageModel)
  2. {
  3. return View(pageModel);
  4. }
复制代码

三、路由实现传参(get/post通用):

  1. 127.0.0.1/Index/MenuDelAsync/1
复制代码

  1. public async Task<string> MenuDelAsync(long id)
  2. {
  3. string jsonResult = "[]";
  4. bool b = false;
  5. b = await articleService.DelArticleTypeAsync(id);
  6. if (b)
  7. jsonResult = CommonHelper.NewGetJsonResult(1, "删除成功");
  8. else
  9. jsonResult = CommonHelper.NewGetJsonResult(-1, "删除失败");
  10. return jsonResult;
  11. }
复制代码

别的用法接待留言增补,谢谢!

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则