找不到Angular2 POST Web API 404 [英] Angular2 POST Web api 404 Not Found

查看:79
本文介绍了找不到Angular2 POST Web API 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Angular2服务来记录存储在ILog对象中的某些事件,并将它们发送到API以存储在数据库中.

I'm building an Angular2 service to log certain events, stored in ILog objects, and send them to an API to be stored in a database.

我的日志服务非常简单:

My log service is pretty straightforward:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { EnvironmentModule } from "../environment";
import { ILog } from "./log";


@Injectable()
export class LogService {

   constructor(private _http: Http, private environment: EnvironmentModule) { }

   postLog(log: ILog): void {
       this.json = this.convertToJSON(log);
       this._http.post(this.environment.getWebApiUri() + 'api/Log/PostLog/', log, {})
           .subscribe(
           () => console.log('Success')
       ); //goes to localhost:3304/WebAPI/Log/PostLog/, returns 404 not found
   }

}

它调用WebAPI控制器,该控制器将数据传递给要处理的服务:

And it calls a WebAPI Controller that passes the data off to a service to be processed:

[RoutePrefix("api/Log")]
public class LogController : ApiController
{
    private ILogService _LogService;

    public LogController() : this(new LogService())
    {

    } //constructor

    public LogController(ILogService LogService)
    {
        _LogService = LogService;
    } //constructor

    [HttpPost()] 
    [Route("PostLog")]
    public void PostLog(Log log)
    {
        _LogService.PostLog(log);
    } //PostLog

} //class

但是,当我的服务调用API时,它会引发 404 Not Found 错误.

Yet, when my service calls the API it throws a 404 Not Found Error.

导航到浏览器中的路径,我看到以下内容:

Navigating to the path in the browser I see this:

<Error>
 <Message>
      No HTTP resource was found that matches the request URI
  'http://localhost:3304/WebAPI/api/Log/PostLog/'.
 </Message>
 <MessageDetail>
      No action was found on the controller 'Log' that matches the request.
 </MessageDetail>
</Error>

有人可以帮我吗?我不明白为什么会这样.

Can anyone help me with this? I don't understand why it's behaving this way.

推荐答案

这是因为您不能将原子值作为json直接发布到您的方法中.您可以将其变成一个对象,然后将其发布为相应的对象,或者以uri编码形式发布,也可以使用.这是asp.net的网络api的局限性.

Its because you cant post an atomic value directly to your method as json. You could turn it into an object and then post it as a corresponding object or post it as form uri encoded which also works. This is a limitation of asp.net's web api.

还有其他一些类似的问题,它们都有相似的答案.这是一个简单的示例,说明了如何更改它的工作方式.

There are some other similar questions all with similar answers. Here is a quick example of how you could change it to work.

c#代码

[HttpPost()] 
[Route("PostLog")]
public void PostLog(LogContainerModel logModel)
{
    _LogService.PostLog(logModel.log);
}

// model
public sealed class LogContainerModel {
    public string log { get; set; }
}

javascript代码

javascript code

private convertToJSON(log: ILog): string {
    return JSON.stringify({log: log});
}


选项2

根据之前的 SO答案将其字符串化为对象.

c#代码

[HttpPost()] 
[Route("PostLog")]
public void PostLog([FromBody] string jsonString)

javascript代码

javascript code

private convertToJSON(log: ILog): string {
    return JSON.stringify({'': log}); // if that does not work try the snippet below
    // return JSON.stringify({'': JSON.stringify(log)});
}


选项3

以下是 bizcoder.com

使用 HttpResponseMessage

[HttpPost()] 
[Route("PostLog")]
public async Task PostLog(HttpRequestMessage request)
{
    var jsonString = await request.Content.ReadAsStringAsync();
    _LogService.PostLog(jsonString);
}

或使用 json.net

[HttpPost()] 
[Route("PostLog")]
public void PostLog([FromBody]JToken jsonbody)
{
    var jsonString = jsonbody.ToString();
    _LogService.PostLog(jsonString);
}

  • SO-如何发布单个字符串(使用表单编码)
  • SO-发送单个字符串参数-这可能是更好的选择,很好的答案.
    • SO - How to post a single string (using form encoding)
    • SO - send single string parameter - this might be the better option, good answer.
    • 这篇关于找不到Angular2 POST Web API 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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