gpt4 book ai didi

c# - 从 Angular 发送到 c# api 失败

转载 作者:行者123 更新时间:2023-11-30 22:51:36 25 4
gpt4 key购买 nike

我正在发送到 c# api。其中一个功能未发送到 api。你能告诉我是什么原因吗? (此 api 的其他功能运行良好)。

组件。

    this.loggedUser.aValidateUser(this.newlyRegistered.uMobile).subscribe(res => {
alert("personal details were saved successfully"),
this.active = 2;
}, err => {
alert("There were wrong details. Please make the necessary changes");
}
);
}

Angular 服务:

  aValidateUser(phoneNumber:string) {
return this.HTTP.post('http://localhost:54058/user/ValidateUser', phoneNumber ,this.httpOptions);
}

C# 接口(interface):

  [EnableCors("*", "*", "*")]
[RoutePrefix("user")]
public class UserController : ApiController
{
SenderLogic user = new SenderLogic();

[HttpPost]
[Route("UploadFile")]
public void UploadFile()//Saves passport in local folder
{
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
{


var file = HttpContext.Current.Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + Path.GetFileName(file.FileName);
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), fileName);
file.SaveAs(path);
}
}
}

[Route("ValidateUser")]
[HttpPost]
public bool ValidateUser(string phoneNumber)//check if phone number-(identifier of user) exists
{
if (user.ValidateUser(phoneNumber) == true)
return true;
return false;
}

调试显示服务没有发送到 c# api。而具有相同 api 和函数类型的其他函数运行良好。

编辑确实有效的功能: Angular -

  if (files.length === 0) {
return;
}
let filesToUpload : File[] = files;
const formData = new FormData();
Array.from(filesToUpload).map((file, index) => {
return formData.append('file'+index, file, file.name);
});
this.clientHttp.post('http://localhost:54058/user/UploadFile', formData, {reportProgress: true, observe: 'events'})
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress)
this.progress = Math.round(100 * event.loaded / event.total);
else if (event.type === HttpEventType.Response) {
this.message = 'Upload success.';
}
});
}

C# 接口(interface):

 [HttpPost]
[Route("UploadFile")]
public void UploadFile()//Saves passport in local folder
{
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
{

var file = HttpContext.Current.Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") +
Path.GetFileName(file.FileName);
var path =
Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"),
fileName);
file.SaveAs(path);
}
}
}

最佳答案

希望对您有所帮助的两件事。

  1. 尝试更改

    的签名
    public bool ValidateUser(string phoneNumber)

    public bool ValidateUser([FromBody] string phoneNumber)
  2. Angular 变化:

    return this.HTTP.post('http://localhost:54058/user/ValidateUser', phoneNumber ,this.httpOptions);

    return this.HTTP.post('http://localhost:54058/user/ValidateUser', JSON.stringify(phoneNumber) ,this.httpOptions);

通常避免将基本类型(int、string 等)传递给 POST 方法。例如,将它们包装在模型类中

public class UserPhone {
public string Phone {get; set;}
}

Controller 方法应该这样改变:

public bool ValidateUser([FromBody] UserPhone phoneNumber)

和 Angular 方法:

return this.HTTP.post('http://localhost:54058/user/ValidateUser', {phone: phoneNumber} ,this.httpOptions);

关于c# - 从 Angular 发送到 c# api 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com