分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

asp.net core MVC 控制器,接收参数,数据绑定

发布时间:2023-09-06 02:27责任编辑:沈小雨关键词:MVC

参数

HttpRequest

HttpRequest 是用户请求对象
QueryString
Form
Cookie
Session
Header
实例:

 ???????public IActionResult Index() ???????{ ???????????QueryString x = Request.QueryString; // ?a=1 ???????????string x = Request.Query["a"]; //1 ???????????return View(); ???????}

HttpContext

HttpContext 是用户请求上下文
提供Session属性获取Session对象
Session.Set设置
Session.Remove移除
Session.TryGetValue获取数据

数据绑定

默认绑定方式,使用特性:

[FromBody] 请求体
[FromHeader] headers
[FromQuery] 查询字符串
[FromRoute] 路由数据
[FromForm] 表单数据
[FromServices] 服务注册

FromHeader 案例

前台:

<div style="height:100px"> ???<input type="button" value="提交带header参数" onclick="save()" /></div><script> ???function save() { ???????$.ajax({ ???????????url: "home/index", ???????????beforeSend: function (xhr) { ???????????????xhr.setRequestHeader("username", "tangsansan"); ???????????}, ???????????type:"post", ???????????success: function(data) { ???????????????????????????} ???????}); ???}</script>

后台:

 ???????public IActionResult Index([FromHeader] string username) ???????{ ???????????QueryString x = Request.QueryString; ???????????return View(); ???????}

asp.net core MVC 控制器,接收参数,数据绑定

原文地址:https://www.cnblogs.com/tangge/p/10153413.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved