zl程序教程

您现在的位置是:首页 >  后端

当前栏目

asp.net core 3.x ApiController可以自动推断参数;自动验证模型状态,如果验证不通过,会自动返回400 ,前后端传参get,post

NetASPCore状态自动 模型 参数 通过
2023-09-11 14:13:57 时间

vue get 传参:

axios.get("http://localhost:44326/home",{params:{i:222}).then(function (res){
console.log(res.data);
}

 

Vue post传参:

1.拼接字符串
var postData='name=${userName}'

2.实例化URLSearchParams
var postData = new URLSearchParams();
postData.append("name",userName);
postData.append("age",19);
axios.post("http://localhost:44326/home",postData).then(function (res){
console.log(res.data);
}

3.直接引用qs.js
安装qs.js,
引用qs.js,

axios.post("http://localhost:44326/home",qs.stringify({name:"name",age:19})).then(function (res){
console.log(res.data);
}

 

4. c# 不加apicontroller 的调用方法:
[HttpPost]
public string GetName(string name,int age)
{
return "post test";
}

[HttpGet]
public string GetName(int i)
{
return "get test ,i ";
}

5. c# 加apicontroller 的调用方法:可以自动推断参数;自动验证模型状态,如果验证不通过,会自动返回400
[HttpPost]
[ApiController]
public string GetName(UserInputModel userinput)
{
return "post test";
}

[HttpGet]
public string GetName(int i)
{
return "get test ,i ";
}