ASP.NET核心(版本:2.2.102)
我正在构建一个API以返回Portos和Species,但每当我访问/API/Portos(在控制器中定义)时,我都会遇到以下错误:
InvalidOperationException:无法解析类型的服务
“鱼。控制器。端口控制器”。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
sp,类型类型,类型要求者,布尔isDefaultParameterRequired)
我不确定我做错了什么,所以非常感谢您的帮助。
模型
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace fish.Models
{
[Table("Especies")]
public class Especie
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Nome { get; set; }
public Porto Porto { get; set; }
public int PortoId { get; set; }
}
}
波尔图
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
namespace fish.Models
{
public class Porto
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Nome { get; set; }
public ICollection<Especie> Models { get; set; }
public Porto()
{
Models = new Collection<Especie>();
}
}
}
控制器
PortoController.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using fish.Controllers.Resources;
using fish.Models;
using fish.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace fish.Controllers
{
public class PortosController : Controller
{
private readonly FishDbContext context;
private readonly IMapper mapper;
public PortosController(FishDbContext context, IMapper mapper)
{
this.mapper = mapper;
this.context = context;
}
[HttpGet("/api/portos")]
public async Task<IEnumerable<PortoResource>> GetPortos()
{
var portos = await context.Portos.Include(m => m.Models).ToListAsync();
return mapper.Map<List<Porto>, List<PortoResource>>(portos);
}
}
}
PortoResources.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace fish.Controllers.Resources
{
public class PortoResource
{
public int Id { get; set; }
public string Nome { get; set; }
public ICollection<EspecieResource> Models { get; set; }
public PortoResource()
{
Models = new Collection<EspecieResource>();
}
}
}
特别资源
namespace fish.Controllers.Resources
{
public class EspecieResource
{
public int Id { get; set; }
public string Nome { get; set; }
}
}
Stratup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper();
services.AddDbContext<FishDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
MappingProfile.cs
using AutoMapper;
using fish.Controllers.Resources;
using fish.Models;
namespace fish.Mapping
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Porto, PortoResource>();
CreateMap<Especie, EspecieResource>();
}
}
}
using fish.Models;
using Microsoft.EntityFrameworkCore;
namespace fish.Persistence
{
public class FishDbContext : DbContext
{
public FishDbContext(DbContextOptions<FishDbContext> options) : base(options)
{
}
public DbSet<Porto> Portos { get; set; }
}
}