Har*_*rry 5 c# asp.net-core asp.net-core-webapi angular
我试图将一些JSON发布到web api .net核心但由于某种原因,无论我尝试过什么,都会看到下面的代码.
这是我的.net核心web api代码
[Produces("application/json")]
[Route("api/v1/Issues")]
[Authorize]
[EnableCors("AllowCors")]
public class IssuesController : Controller
{
[HttpPost]
public IActionResult PostIssues([FromBody] string issue)
{
//some code to desirialize
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我试图通过角度发布的方式(注意加载了相应的服务)
public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {
headers.append('Authorization', `Bearer ${t.toString()}`);
headers.append('Content-Type', 'application/json')
this.http.post(`${this.baseUrl}/v1/issues`, issue, {
headers: headers
}).toPromise()
.then(res => {
console.log(res.json());
});
});
}
Run Code Online (Sandbox Code Playgroud)
我试过把帖子改成了
this.http.post(`${this.baseUrl}/v1/issues`, JSON.stringify(issue))
Run Code Online (Sandbox Code Playgroud)
乃至
this.http.post(`${this.baseUrl}/v1/issues`, {'issue', JSON.stringify(issue)})
Run Code Online (Sandbox Code Playgroud)
但似乎没有任何工作,任何人都知道为什么会发生这种情况?澄清string issueAPI上收到的内容始终为null.这是成功的响应,当我到达服务器时,您可以看到请求有效负载不为null但是到达web api为null

更新1如果可能需要说明 好吧我想做一个小实验,相同的代码适用于4.5框架而不是.net核心但是我试图为.net核心和工作做的是创建一个名为foo的类,见下文
public class Foo
{
public string Name { get; set; }
public string Json { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并修改我的控制器以接受此对象
[HttpPost]
public IActionResult PostIssues([FromBody] Foo issue)
{
Debug.Write(issue);
return Ok(issue);
}
Run Code Online (Sandbox Code Playgroud)
以及我的Angular项目传递类似的格式体
public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {
headers.append('Authorization', `Bearer ${t.toString()}`);
headers.append('Content-Type', 'application/json');
this.http.post(`${this.baseUrl}/v1/issues`, {'name' : 'name', 'json': JSON.stringify(issue)}, {
headers: headers,
withCredentials: true
}).toPromise()
.then(res => {
console.log(res.json());
});
});
Run Code Online (Sandbox Code Playgroud)
}
为什么这个好但是不能直接传递Json?为什么我的原始代码适用于4.5框架但不适用于.net核心?
我会尽力回答你所有的问题。
API 上收到的字符串问题始终为空
向此传递 JSON 总是会得到 null,因为您正在尝试绑定到原始数据类型。要解决此问题,您有两种选择
绑定到模型(viewmodel)
当你的参数是一个类时,asp会将这个类绑定到你传递的json数据上,这就是你能够获取数据的原因name。根据您的更新,您可能会走这条路。
更多信息:https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding
现在您可能想知道,当参数只是一个字符串时,为什么要创建一个仅在 api 上使用的类?!如果你只想string在你的 api 中使用那么你可以这样做
使用传递数据FormData
你需要像这样改变你的api
public IActionResult PostIssues(string issue){..}
Run Code Online (Sandbox Code Playgroud)
您无需在[FromBody]此处指定。
在你的客户端,
public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {
headers.append('Authorization', `Bearer ${t.toString()}`);
// new code here
var formData = new FormData();
formData.append('issue',issue); //you need to specify the parameter name here.
this.http.post(`${this.baseUrl}/v1/issues`, formData, {
headers: headers
}).toPromise()
.then(res => {
console.log(res.json());
});
});
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
3866 次 |
| 最近记录: |