相同的 HttpPut 请求代码格式不起作用,但适用于另一个请求



我有一个未被调用的HTTPPUT请求。我有一个类似的放置请求来管理另一个选项卡并且它可以工作。两个页面非常相似。我不知道我做错了什么。

我几乎尝试了所有方法,但不知道还能尝试什么。

控制器:

   [HttpPut]
        [Route("updateAllocations({type})")]
        public IHttpActionResult UpdateAllocations(string type, T_LOC entity)
        {
            System.Diagnostics.Debug.WriteLine("inside");
            _allocationsService.UpdateAllocations(type,entity);
            return Ok();
        }

接口:

using OTPS.Core.Objects;
using System.Collections.Generic;
using OTPS.Core.Models;
namespace OTPS.Core.Interfaces
{
    public interface IAllocationsService
    {
        void UpdateAllocations(string type,  T_LOC entity);
    }
}

服务:

  public void UpdateAllocations(string type, T_LOC entity)
        {
            System.Diagnostics.Debug.WriteLine("inside");
        }

客户端:

 public updateAllocation(type: string , entity) {
        console.log("sdfsdf")
        console.log(`${this.baseUrl}/api/allocations/updateAllocations(${type})`)
        return this.http.put(`${this.baseUrl}/api/allocations/updateAllocations({type})`, entity, { headers: this.headers, withCredentials: true })
            .pipe(catchError((error: Error) => {
                console.log("sdfasd111111sdf")
                return this.errorService.handleError(error);
            }));
    }

我希望 clinet 端在进行任何进一步的逻辑之前调用 put 请求,但服务器端的打印永远不会被调用。.

确保subscribe组件内部的服务方法:

this.myService.updateAllocation(type, entity).subscribe( response => {
// do something here with response
});

您必须呼叫subscribe()否则什么都不会发生。只是打电话 服务方法不会启动放置/删除/发布/获取请求。

始终订阅!

HttpClient 方法不会开始其 HTTP 请求,直到您调用 subscribe()该方法返回的可观察量。这是真的 适用于所有HttpClient方法。