为什么我的 API 调用没有通过邮递员触发? MVC1005警告

我正在尝试进行 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


紫衣仙女
浏览 71回答 3
3回答

湖上湖

解决方案是这样的:public void ConfigureServices(IServiceCollection services) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; services.AddMvc();&nbsp; &nbsp; services.AddControllers(); //added this&nbsp; &nbsp; var connectionString = Configuration["connectionStrings:bookDbConnectionString"];&nbsp; &nbsp; services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));&nbsp; &nbsp; services.AddScoped<ICountryRepository, CountryRepository>();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context) {&nbsp; &nbsp; if (env.IsDevelopment()) {&nbsp; &nbsp; &nbsp; &nbsp; app.UseDeveloperExceptionPage();&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; app.UseRouting(); //uncommented&nbsp;&nbsp; &nbsp; app.UseAuthorization(); //added this&nbsp; &nbsp; app.UseEndpoints(endpoints => { //added this&nbsp; &nbsp; &nbsp; &nbsp; endpoints.MapControllers();&nbsp; &nbsp; });&nbsp; &nbsp; //removed the app.UseMvc(); line}

繁花如伊

结合不同的方法来实现这一目标[Route("api/country")][ApiController]public class CountryController : Controller{&nbsp; &nbsp; private ICountryRepository countryRepository;&nbsp; &nbsp; public CountryController(ICountryRepository repository)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.countryRepository = repository;&nbsp; &nbsp; }&nbsp; &nbsp; [HttpGet]&nbsp; &nbsp; public IActionResult GetCountries()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var countries = countryRepository.GetCountries().ToList();&nbsp; &nbsp; &nbsp; &nbsp; return Ok(countries);&nbsp; &nbsp; }&nbsp;}或者[Route("api/[controller]")][ApiController]public class CountryController : Controller{&nbsp; &nbsp; private ICountryRepository countryRepository;&nbsp; &nbsp; public CountryController(ICountryRepository repository)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.countryRepository = repository;&nbsp; &nbsp; }&nbsp; &nbsp; [HttpGet("")]&nbsp; &nbsp; public IActionResult GetCountries()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var countries = countryRepository.GetCountries().ToList();&nbsp; &nbsp; &nbsp; &nbsp; return Ok(countries);&nbsp; &nbsp; }&nbsp;}我最喜欢这个,我认为它是最直观的[Route("api")][ApiController]public class CountryController : Controller{&nbsp; &nbsp; private ICountryRepository countryRepository;&nbsp; &nbsp; public CountryController(ICountryRepository repository)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.countryRepository = repository;&nbsp; &nbsp; }&nbsp; &nbsp; [HttpGet("country")]&nbsp; &nbsp; public IActionResult GetCountries()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var countries = countryRepository.GetCountries().ToList();&nbsp; &nbsp; &nbsp; &nbsp; return Ok(countries);&nbsp; &nbsp; }&nbsp;}如果这些都不起作用,那是因为您没有在 中的 方法中调用 app.UseMVcConfigureStartup.cs

小唯快跑啊

问题是路线,必须继续行动[HttpGet][Route("api/[controller]")]public IActionResult GetCountries()或者,您可以将路由保留在控制器上,然后将一个空路由添加到操作中:[Route("")]
打开App,查看更多内容
随时随地看视频慕课网APP