我正在尝试进行 API 调用;我启动 Visual Studio 并在邮递员中输入:
http://localhost:51266/api/country
我在该方法上设置了断点,但没有任何反应。我得到了 404 未找到。
这是控制器:
[Route("api/[controller]")]
[ApiController]
public class CountryController : Controller
{
private ICountryRepository countryRepository;
public CountryController(ICountryRepository repository)
{
this.countryRepository = repository;
}
[HttpGet]
public IActionResult GetCountries()
{
var countries = countryRepository.GetCountries().ToList();
return Ok(countries);
}
}
我在这里做错了什么?
我在 Startup 中有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connectionString = Configuration["connectionStrings:bookDbConnectionString"];
services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));
services.AddScoped<ICountryRepository, CountryRepository>();
}
我现在有这样的:
[ApiController]
public class CountryController : Controller
{
private ICountryRepository countryRepository;
public CountryController(ICountryRepository repository)
{
this.countryRepository = repository;
}
[HttpGet]
[Route("api/[controller]")]
public IActionResult GetCountries()
{
var countries = countryRepository.GetCountries().ToList();
return Ok(countries);
}
}
和我的启动课程:
public class Startup
{
public static IConfiguration Configuration { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
我收到这个警告:
警告 MVC1005
使用“UseMvc”使用端点路由时不支持配置 MVC。要继续使用“UseMvc”,请设置“MvcOptions.EnableEndpointRouting = false”在“配置服务”内。 WebApplication2 D:\Mijn Documents\VisualStudio_2019\WebApplication2\WebApplication2\Startup.cs
湖上湖
繁花如伊
小唯快跑啊
相关分类