Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net httpGet获取httpstatus 415,用于使用复杂类型参数调用控制器方法_C#_Angular_Asp.net Core_.net Core - Fatal编程技术网

C# Asp.Net httpGet获取httpstatus 415,用于使用复杂类型参数调用控制器方法

C# Asp.Net httpGet获取httpstatus 415,用于使用复杂类型参数调用控制器方法,c#,angular,asp.net-core,.net-core,C#,Angular,Asp.net Core,.net Core,我有一个angular客户端,它正在向服务器发送HttpRequest以获取客户端信息: getClient(roleId): Observable<ClientDto> { let params = new HttpParams(); params = params.append('Id', clientId); return this._httpClient.get<ClientDto>('myurl', {params})

我有一个angular客户端,它正在向服务器发送HttpRequest以获取客户端信息:

 getClient(roleId): Observable<ClientDto> {

      let params = new HttpParams();
      params = params.append('Id', clientId); 

      return this._httpClient.get<ClientDto>('myurl', {params});
    }
getClient(roleId):可观察{
设params=newhttpparams();
params=params.append('Id',clientId);
返回此参数。httpClient.get('myurl',{params});
}
在控制器上我有

    [HttpGet]
    public async Task<ActionResult<ClientDto>> GetClient(EntityDto<int> data)
    {
        return Ok(await clientsService.GetClient(data.Id));
    }

    public class EntityDto : EntityDto<int>, IEntityDto
    {
        public EntityDto()
        {

        }

        public EntityDto(int id)
            : base(id)
        {
        }
    }

    public class EntityDto<TKey> : IEntityDto<TKey>
    {
        [Required]
        public TKey Id { get; set; }

        public EntityDto()
        {

        }

        public EntityDto(TKey id)
        {
            Id = id;
        }
    }
 public interface IEntityDto : IEntityDto<int>
    {

    }

    public interface IEntityDto<TKey>
    {
        /// <summary>
        /// Id of the entity.
        /// </summary>
        TKey Id { get; set; }
    }
[HttpGet]
公共异步任务GetClient(EntityDto数据)
{
返回Ok(wait clientsService.GetClient(data.Id));
}
公共类EntityDto:EntityDto,IEntityDto
{
公共实体
{
}
公共实体到(int id)
:base(id)
{
}
}
公共类实体收件人:IEntityDto
{
[必需]
公钥Id{get;set;}
公共实体
{
}
公共实体DTO(TKey id)
{
Id=Id;
}
}
公共接口IEntityDto:IEntityDto
{
}
公共接口IEntityDto
{
/// 
///实体的Id。
/// 
TKey Id{get;set;}
}
当我从angular发出呼叫时,我收到Http状态415响应
有人知道为什么会发生这种情况吗?

一般来说,将您请求的资源的ID添加到请求路径是一个好主意,例如:
/books/1
这将返回ID为1的图书,您将ID添加为查询字符串有何原因

要回答您的评论:查询字符串通常用于筛选或分页,默认情况下不进行搜索,您需要添加
[FromQuery]

我建议将您的资源id作为您请求的一部分发送

您可以这样检索它:

[HttpGet("{id})"]
public async Task<ActionResult<ClientDto>> GetClient(int id, [FromQuery] string fields)
{
}
[HttpGet({id})]]
公共异步任务GetClient(int id,[FromQuery]字符串字段)
{
}
在此,id包含在路由中

例如:
/client/10


您想要的任何筛选都包含在查询字符串中(您的angular
HttpParams
),例如,如果您只需要资源中的一个字段,您可以将其添加为查询字符串,我将假设您使用的是
[ApicController]
,我认为您必须这样才能看到您描述的行为。使用此属性后,复杂类型的模型绑定规则将从默认值更改

这就好像您在
EntityDto
属性上指定了
[FromBody]
。当应用
[FromBody]
时,模型绑定过程将查看随请求提供的
内容类型
头。选择输入格式化程序时使用此标头的值-在最简单的级别上,它将与json输入格式化程序匹配,例如
application/json
。如果报头丢失或不包含支持的值,则发送415不支持的媒体类型响应

您已经知道解决方案,即使用绑定源属性覆盖推理:

public async Task<ActionResult<ClientDto>> GetClient([FromQuery] EntityDto<int> data)
公共异步任务GetClient([FromQuery]EntityDto数据)

我希望这个答案可以解释为什么需要这样做。

也许您需要在请求上指定内容类型头,比如“application/json”,现在我收到了错误的请求(400),我添加了[FromQuery],结果成功了。为什么我需要为Get请求指定此选项?没有任何方法可以跳过添加[FromQuery],是吗?没有简单的方法可以做到这一点。有一个更高级的选项涉及到,但这将涉及比仅使用
[FromQuery]
更多的工作。