首页 > 解决方案 > Angular 将字符串发布到 asp.net 后端

问题描述

我有一个带有 asp.net 后端的 Angular 项目。我喜欢做的是,简单地将字符串发布到我的控制器。

我尝试了以下方法:

constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { }

httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json'
  })
};

postString(data:string) {
  this.http.post(this.baseUrl + 'api/utility/GetGuid/', "test", this.httpOptions)
  .subscribe((guid: string) => {
     //do something
  });
}

后端:

[Route("api/[controller]")]
public class UtilityController : Controller
{

    [HttpPost]
    [Route("GetGuid")]
    public JsonResult GetGuid([FromBody]string data)
    {
       //do something
    }

这似乎不起作用。不知道如何实现,我只是发布字符串并在后端获取它。

希望您能够帮助我。

标签: angularstringtypescriptpostbackend

解决方案


尝试设置标题Content-Type: application/json; charset=utf-8

参考: https ://weblog.west-wind.com/posts/2013/dec/13/accepting-raw-request-body-content-with-aspnet-web-api


推荐阅读