邮递员不会触发api调用

时间:2019-09-20 15:47:54

标签: c# rest asp.net-core asp.net-web-api postman

我正在尝试获取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;
    }

    // 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();
    }
}

如果我这样做:

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

        //app.UseRouting();

        app.UseMvc();
}

我收到此警告:

  

警告MVC1005
  使用端点路由时,不支持使用“ UseMvc”来配置MVC。要继续使用“ UseMvc”,请在“ ConfigureServices”中设置“ MvcOptions.EnableEndpointRouting = false”。 WebApplication2 D:\ Mijn Documents \ VisualStudio_2019 \ WebApplication2 \ WebApplication2 \ Startup.cs

3 个答案:

答案 0 :(得分:0)

问题是路线,必须继续采取行动

[HttpGet]
[Route("api/[controller]")]
public IActionResult GetCountries()

或者,您可以将路由保留在控制器上,然后在操作中添加一个空的

[Route(“”)]

答案 1 :(得分:0)

结合不同的方法来实现这一目标

[Route("api/country")]
[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);

    }
 }

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

    }
 }

尽管我最喜欢这个,我认为它最直观

[Route("api")]
[ApiController]
public class CountryController : Controller
{

    private ICountryRepository countryRepository;

    public CountryController(ICountryRepository repository)
    {
        this.countryRepository = repository;
    }

    [HttpGet("country")]
    public IActionResult GetCountries()
    {

        var countries = countryRepository.GetCountries().ToList();

        return Ok(countries);

    }
 }

如果这些都不起作用,那是因为您没有在app.UseMVc的{​​{1}}方法中调用Configure

答案 2 :(得分:0)

啊,老天。我看了好几个小时。解决方案是这样的:

public void ConfigureServices(IServiceCollection services)
        {           

            services.AddMvc();

            services.AddControllers();


            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();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });


        }