我想在一个有两个参数的动作上发布数据,我可以实现。我在asp.net mvc 5应用程序中使用angular 2。
public ActionResult Login(string returnUrl, UserLogin _user)
{
return RedirectToAction("ExternalSignIn", new { returnUrl = returnUrl, userEmail = _user.Email });
}
答案 0 :(得分:5)
首先确保输入以下内容:
import { Http, Headers } from 'angular2/http';
然后这是您的服务电话
let url = 'base api url/Account/Login';
let data = {
"returnUrl": this.returnUrl,
"email": this.email
};
let body = JSON.stringify(data )
let head = new Headers({
'Content-Type': 'application/json'
});
this.http.post(url, body , {headers : head})
.map(res => res.json())
.subscribe((data:any) => {
//subscribe actions
});
答案 1 :(得分:1)
floor: Floor = { id: 0, name: "", floorNumber: 0, addressNumber: "", floorTypeid: 0 };
floorUnit: FloorUnit = { id: 0, floorID: 0, unitID: 0, addressNumber: '',unitCost: 0, standardPrice: 0, taxTermid: 0, imageUrl: '', planUrl: '', isAvailable: true, isTemperoryBooking: false, isNotReleased: false };
const floorUnitAddURL = "api/floorunit/AddFloorUnit";
// you can pass two class objects in same post method by array as follows
AddFloorUnit() {
if (this.validationService.Validate(this.floorUnitForm, this.floorUnitFormErrors, this.floorUnitValidationMessages))
{
this.apiService.post(floorUnitAddURL, [this.floorUnit, this.floor], true).subscribe(
res => {
var data: any = res;
if (data && data.status.success) {
this.notificationService.NotifyError("Success", data.status.displayMessage, "success");
//this.ResetPropertyForm();
}
else
this.notificationService.NotifyError("Error", data.status.displayMessage, "warning");
},
err => {
this.notificationService.NotifyError("Error", "Error Occured.", "warning");
});
}
};
在Web API方法中有两个单独的参数
[HttpPost("AddFloorUnit")]
public JsonResult Post([FromBody]FloorUnitDTO floorUnit , [FromBody]FloorDTO floor)
{
try
{
Floor updateFloor = Mapper.Map<Floor>(floor);
Proxy<Floor>.SetBaseEntityProperties(Request, updateFloor, true);
_floorService.UpdateFloor(updateFloor);
FloorUnit newfloorUnit = Mapper.Map<FloorUnit>(floorUnit);
Proxy<FloorUnit>.SetBaseEntityProperties(Request, newfloorUnit);
_floorUnitService.AddFloorUnit(newfloorUnit);
return Json(ErrorHandle<FloorUnit>.GetSuccessMessage(null));
}
catch (Exception ex)
{
Log4netLogger<FloorUnit>.Error(Constants.ExceptionPolicy.BusinessLogic, ex);
return Json(ErrorHandle<FloorUnit>.GetInsertErrorMessage(ErrorCodes.FloorUnitInsertError, typeof(FloorUnit).Name));
}
}
答案 2 :(得分:0)
之前我遇到过同样的问题,我通过对后端代码进行修改来解决。
相反,在函数API中有2个参数,我创建了一个新参数作为属性的模型。
在:
[HttpPost]
public ActionResult ChangingStatusAS(int id, int action)
{
...
}
后:
public class PostChangingStatusAS
{
public int id { get; set; }
public int action { get; set; }
}
[HttpPost]
public ActionResult ChangingStatusAS(PostChangingStatusAS param)
{
...
}
使用Angular进行服务调用:
let data = {
id: 25,
action: 25
};
let body = JSON.stringify(data)
let head = new Headers({
'Content-Type': 'application/json'
});
this._http.post(url,body , {headers : head})
.map(res => res.json())
.subscribe((data:any) => {
//subscribe actions
});
答案 3 :(得分:0)
你需要创建一个类来将多个参数传递给一个帖子,如果你想传递独立的参数,它就在post方法的定义中。
Angular post只接受身体前的对象。
this._http.post(url, {moo:"foo",goo:"loo"}).....