首页 > Web开发 > 详细

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

时间:2018-12-21 00:26:18      阅读:542      评论:0      收藏:0      [点我收藏+]

参数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!