Angular 传递给我的控制器的参数总是错误的
Angular parameter being passed to my controller is always false
不确定我的路由或语法是否正确,因为我对 Angular 路由有点陌生。
但是 'isPartial' 参数始终为 false。
这是我在 Angular/Typescript 中调用网络服务控制器的代码。
我正在传递一个 id 和一个布尔值 'isPartial',id 正常但 isPartial 始终为 false
// .ts file
this.webService.add(this.claim.id, true)
.subscribe(result => {
// do some stuff
}, error => {
// take care of error
});
// web service
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, isPartial);
}
// my route
{
path: 'claim/:id',
component: ClaimComponent,
resolve: {
claim: ClaimResolver
},
canDeactivate: [PreventUnsavedChanges]
},
这是我的控制器
[Route("api/[controller]")]
[ApiController]
public class WebServiceController : ControllerBase
{
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, bool isPartial)
{
// isPartial is always false
}
}
像这样更改 C# 控制器中的参数绑定
[Route("api/[controller]")]
[ApiController]
public class WebServiceController : ControllerBase
{
[HttpPost("add/{id}")]
public async Task<IActionResult> Add( [FromUri]int id, [FromBody]bool isPartial)
{
// isPartial is always false
}
}
我已经为您的情况配置了两种可能的解决方案。
解决方案 1:将您的请求 Content-Type
设置为 application/json
而不是默认的 Content-Type: text/plain;charset=UTF-8
ts:
import { HttpClient, HttpHeaders } from '@angular/common/http';
//...
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, isPartial, httpOptions);
}
控制器:
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, [FromBody]bool isPartial)
方案二:将isPartial
包裹在一个模型中,如果有多个参数建议使用
ts:
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, { 'isPartial': isPartial });
}
http.post(baseUrl + 'api/SampleData/add/' + id, { 'isPartial': isPartial })
控制器:
public class myModel
{
public bool isPartial { get; set; }
}
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, [FromBody]myModel myModel)
不确定我的路由或语法是否正确,因为我对 Angular 路由有点陌生。 但是 'isPartial' 参数始终为 false。
这是我在 Angular/Typescript 中调用网络服务控制器的代码。 我正在传递一个 id 和一个布尔值 'isPartial',id 正常但 isPartial 始终为 false
// .ts file
this.webService.add(this.claim.id, true)
.subscribe(result => {
// do some stuff
}, error => {
// take care of error
});
// web service
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, isPartial);
}
// my route
{
path: 'claim/:id',
component: ClaimComponent,
resolve: {
claim: ClaimResolver
},
canDeactivate: [PreventUnsavedChanges]
},
这是我的控制器
[Route("api/[controller]")]
[ApiController]
public class WebServiceController : ControllerBase
{
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, bool isPartial)
{
// isPartial is always false
}
}
像这样更改 C# 控制器中的参数绑定
[Route("api/[controller]")]
[ApiController]
public class WebServiceController : ControllerBase
{
[HttpPost("add/{id}")]
public async Task<IActionResult> Add( [FromUri]int id, [FromBody]bool isPartial)
{
// isPartial is always false
}
}
我已经为您的情况配置了两种可能的解决方案。
解决方案 1:将您的请求 Content-Type
设置为 application/json
而不是默认的 Content-Type: text/plain;charset=UTF-8
ts:
import { HttpClient, HttpHeaders } from '@angular/common/http';
//...
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, isPartial, httpOptions);
}
控制器:
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, [FromBody]bool isPartial)
方案二:将isPartial
包裹在一个模型中,如果有多个参数建议使用
ts:
add(id: number, isPartial: boolean): Observable <any> {
return this.http.post(this.baseUrl + 'webservice/add/' + id, { 'isPartial': isPartial });
}
http.post(baseUrl + 'api/SampleData/add/' + id, { 'isPartial': isPartial })
控制器:
public class myModel
{
public bool isPartial { get; set; }
}
[HttpPost("add/{id}")]
public async Task<IActionResult> Add(int id, [FromBody]myModel myModel)