asp.net core 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();
        }

猜你喜欢

转载自www.cnblogs.com/tangge/p/10153413.html
今日推荐