Asp.net核心Webapi从Angular4应用获取空值 [英] Asp.net core webapi getting null value posted from Angular4 app

查看:80
本文介绍了Asp.net核心Webapi从Angular4应用获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular4的新手,可以快速交付东西,所以没有时间充分学习它,所以请原谅我的问题:

I am new to Angular4 and in a situation to deliver the stuff quickly so have no time to learn it thoroughly so pardon if my question seems childish:

在我的Asp.Net Web API中,我有Confirmemail API,必须从Angular4应用程序中调用它,如下所示:

From my Asp.Net Web API I have Confirmemail API which has to be called from Angular4 application, looks like this:

Asp.net WebApi:

[HttpPost]
public async Task<object>ConfirmEmail([FromBody] string userid,[FromBody] string code)
{
}

在Angular4 Service API中:

ConfirmEmail(userid,code)
{           
    let url =`api/account/ConfirmEmail`;
    let data= `userid=${userid}&code=${code}`;
    console.log(data);
    return this.http.post(url,data);
}

在这里,我在console.log中检查了数据是否正确,但是在webapi端,我发现这些字符串为null.我尝试删除[FromBody],但对我而言无效.

Here, I checked in console.log the data is coming properly, but at the webapi side I am finding these strings null. I tried removing [FromBody] but it didn't worked with me.

我真的不知道所缺少的是什么,准备所有这些东西几乎是一天,但没有取得任何成功.你们有什么解决方法吗?

I am really clueless what is missing, it is almost one day preparing all these things but have not got any success. Do you guys have any workaround?

推荐答案

对于从角度应用程序到APIpost数据,请尝试在角度中进行以下操作:

For post data to your API from angular app try this in angular:

let url ="api/account/ConfirmEmail";

var userInfo = { "userid":userid, "code":code }
let content = JSON.stringify(userInfo);

let headers = new HttpHeaders(); 
headers.set("Content-Type", "application/json");

return this.http.post(url, content, { headers: headers, responseType: 'text'});

,并在您的API中,以接收您的请求正文并反序列化:

and in your API for receive your request body and deserialize this:

[HttpPost]
public async Task<object>ConfirmEmail([FromBody] UserInfo userInfo)
{

}

Public Class UserInfo
{
  public long userid {get; set;}
  public long code {get; set;}
}

要在 url 中发送数据,您应使用http get这样的方法:

For send your data in url you should use http get method like this:

角度:

let Params = new HttpParams();

Params = Params.append("userid", userid);
Params = Params.append("code", code);

return this.http.get(url , { params: Params });

Web API:

[HttpGet]
public async Task<object>ConfirmEmail(string userid, string code)
{
}

这篇关于Asp.net核心Webapi从Angular4应用获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆