Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# 映射器的结果为空?如何将api请求映射到dto?_C#_Mapping_Dto - Fatal编程技术网

C# 映射器的结果为空?如何将api请求映射到dto?

C# 映射器的结果为空?如何将api请求映射到dto?,c#,mapping,dto,C#,Mapping,Dto,我正在调用一个api,然后将该响应反序列化到一个列表,然后尝试响应到一个新列表: public async Task<IEnumerable<RoadDto>> GetRoadStatusDetail() { List<Road> road = await CallApi(); return road .Select(x => _mapper.Map(x)); }

我正在调用一个api,然后将该响应反序列化到一个列表,然后尝试响应到一个新列表:

    public async Task<IEnumerable<RoadDto>> GetRoadStatusDetail()
    {
        List<Road> road = await CallApi();

        return road
            .Select(x => _mapper.Map(x)); 

    }


    private async Task<List<Road>> CallApi()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(baseURL);

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage res = await client.GetAsync(baseURL);

        if (res.IsSuccessStatusCode)
        {
            var roadResponse = res.Content.ReadAsStringAsync().Result;

           var road = JsonConvert.DeserializeObject<List<Road>>(roadResponse);

           return road;
        }

        return null;
    }
我不确定它为什么会为null,我在这里创建了一个映射类来处理它

   public class RoadToRoadDtoMapper : IMapToNew<Road, RoadDto>
{
    public RoadDto Map(Road model)
    {
        return new RoadDto
        {
            DisplayName = model?.DisplayName,
            StatusSeverity = model?.StatusSeverity,
            StatusSeverityDescription = model?.StatusSeverityDescription
        };
    }

}
public class RoadToRoadDtoMapper:IMapToNew
{
公共道路地图(道路模型)
{
返回新道路
{
DisplayName=型号?.DisplayName,
StatusSeverity=型号?。StatusSeverity,
StatusSeverityDescription=型号?.StatusSeverityDescription
};
}
}

错误状态下,
\u mapper
变量为空。将您的映射器接口DI到控制器:

private readonly IMapToNew<Road, RoadDto> _mapper;

// Make sure your controller constructor takes your mapper interface.
public MyController(IMapToNew<Road, RoadDto> mapper)
{
    // Assign the constructor parameter to your instance 
    _mapper = mapper; variable.
}

这回答了你的问题吗?在范围中定义变量是不够的。。。它也需要初始化。Null引用表示从未初始化引发错误的对象,或者该属性未初始化exist@Jawad你能举个例子吗?我不太清楚你的意思?错误表明
\u mapper
为空。确保它被正确注入。这是依赖注入吗?我想我现在明白了,我的制图器注入器没有读取我的制图类“RoadToLoaddToMapper”?
   public class RoadToRoadDtoMapper : IMapToNew<Road, RoadDto>
{
    public RoadDto Map(Road model)
    {
        return new RoadDto
        {
            DisplayName = model?.DisplayName,
            StatusSeverity = model?.StatusSeverity,
            StatusSeverityDescription = model?.StatusSeverityDescription
        };
    }

}
private readonly IMapToNew<Road, RoadDto> _mapper;

// Make sure your controller constructor takes your mapper interface.
public MyController(IMapToNew<Road, RoadDto> mapper)
{
    // Assign the constructor parameter to your instance 
    _mapper = mapper; variable.
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<
        IMapToNew<Road, RoadDto>,
        RoadToRoadDtoMapper>();
}