C# 找不到Angular2 POST Web api 404

C# 找不到Angular2 POST Web api 404,c#,angular,asp.net-web-api,C#,Angular,Asp.net Web Api,我正在构建一个Angular2服务来记录存储在ILog对象中的某些事件,并将它们发送到API以存储在数据库中 我的日志服务非常简单: import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { EnvironmentModule } from "../environment"; import { ILog } from "./log"; @Injectable() exp

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

我的日志服务非常简单:

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控制器,将数据传递给要处理的服务:

[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时,它抛出一个
404notfound
错误

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

<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>

未找到与请求URI匹配的HTTP资源
'http://localhost:3304/WebAPI/api/Log/PostLog/'.
在与请求匹配的控制器“日志”上未找到任何操作。

有人能帮我吗?我不明白它为什么会这样。

这是因为您不能将原子值作为json直接发布到方法中。您可以将其转换为一个对象,然后将其作为相应的对象发布,或者将其作为uri编码的表单发布,这也可以正常工作。这是asp.net的web api的一个限制

还有一些其他类似的问题都有类似的答案。下面是一个如何将其更改为工作的快速示例

c#代码

javascript代码

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

选择2 根据前面的步骤将其字符串化为对象

c#代码

javascript代码

private convertToJSON(log: ILog): string {
    return JSON.stringify({log: log});
}
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 这里有一些来自

使用
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);
}
  • -这可能是更好的选择,好答案

    • 这是因为您不能将原子值作为json直接发布到方法中。您可以将其转换为一个对象,然后将其作为相应的对象发布,或者将其作为uri编码的表单发布,这也可以正常工作。这是asp.net的web api的一个限制

      还有一些其他类似的问题都有类似的答案。下面是一个如何将其更改为工作的快速示例

      c#代码

      javascript代码

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

      选择2 根据前面的步骤将其字符串化为对象

      c#代码

      javascript代码

      private convertToJSON(log: ILog): string {
          return JSON.stringify({log: log});
      }
      
      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 这里有一些来自

      使用
      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);
      }
      
      • -这可能是更好的选择,好答案

      这是因为您不能将原子值作为json直接发布到方法中。您可以将其转换为一个对象,然后将其作为相应的对象发布,或者将其作为uri编码的表单发布,这也可以正常工作。这是asp.net的web api的一个限制。@Igor无论发布json还是ILog对象,我都会收到相同的错误。你有这方面的文档吗?我更新了我的答案,第二个链接提供了一个很好的例子,说明了如何更改javascript代码并使其工作(我认为)。这是因为你不能将原子值作为json直接发布到方法中。您可以将其转换为一个对象,然后将其作为相应的对象发布,或者将其作为uri编码的表单发布,这也可以正常工作。这是asp.net的web api的一个限制。@Igor无论发布json还是ILog对象,我都会收到相同的错误。你有这方面的文档吗?我更新了我的答案,第二个链接有一个很好的例子,说明了如何更改javascript代码并使其工作(我想)。或者,你可以将
      [FromBody]
      添加到action方法的
      jsonString
      参数中,并让客户端代码发布类似
      “{log:\”的内容请求正文中的消息\“}”
      (基本上将日志对象编码为JSON两次)。但是你所描述的方法更好,很有洞察力。不幸的是,即使尝试了所有这些,仍然存在同样的问题ideas@NathanFoss-在选项#3感谢所有帮助@Igor下增加了2个选项。根据那篇文章,我将我的实现更改为直接通过对象。不幸的是,我仍然得到一个包含所有这些选项的
      404
      。@NathanFoss-这可能不仅仅是因为您发送了json。此时,我将设置一个断点并复制字符串
      this.environment.getWebApiUri()+'api/Log/PostLog/'
      ,并确保其正确。然后使用Fiddler测试带有值的url。如果这不起作用,请重试并在没有值的情况下发布(从WebAPI方法中删除值)。这将有助于您确定问题(此时)是url、路由还是您正在传递的参数以外的其他内容。或者,您可以将
      [FromBody]
      添加到action方法的
      jsonString
      参数中,并让您的客户端代码发布类似于
      “{log:\'message\}”的内容
      (基本上将日志对象编码为JSON两次)在请求正文中。但是您描述的方法更好。很棒的见解。不幸的是,即使尝试了所有这些方法,仍然存在相同的问题ideas@NathanFoss-在选项#3感谢所有帮助@Igor下添加了2个选项。根据那篇文章,我正在更改我的实现以直接通过对象。不幸的是,我直到得到一个包含所有这些选项的
      404
      。@NathanFoss-这可能不仅仅是因为您正在发送json。此时,我将设置一个断点并复制字符串
      this.environment.getWebApiUri()+'api/Log/PostLog/'
      并确保其正确无误。然后使用Fiddler测试带有值的url。如果不起作用,请重试并在没有值的情况下发布(从web api方法中删除值)。这将有助于您确定问题(此时)是url、路由还是您传递的参数以外的其他内容。