Search code examples
c#restasp.net-coreasp.net-web-apipostman

Why doesn't my API call trigger with postman? MVC1005 warning


I am trying a make an API call; I start Visual Studio and enter this in postman:

http://localhost:51266/api/country

and I put a breakpoint on the method, but nothing happens. And I get an 404 not found.

This is the controller:

[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);
    }
}

What am I doing wrong here?

And I have this in the Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    var connectionString = Configuration["connectionStrings:bookDbConnectionString"];
    services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));

    services.AddScoped<ICountryRepository, CountryRepository>();
}

I have it now like this:

[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);
    }
}

and my startup class:

public class Startup
{
    public static IConfiguration Configuration { get; set; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        var connectionString = Configuration["connectionStrings:bookDbConnectionString"];
        services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));

        services.AddScoped<ICountryRepository, CountryRepository>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
    }
}

If I do this:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context)
{
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //app.UseRouting();

        app.UseMvc();
}

I get this warning:

Warning MVC1005
Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'. WebApplication2 D:\Mijn Documents\VisualStudio_2019\WebApplication2\WebApplication2\Startup.cs


Solution

  • The solution was this:

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