Веб-API игнорирует атрибут маршрутаC#

Место общения программистов C#
Anonymous
Веб-API игнорирует атрибут маршрута

Сообщение Anonymous »

Я работаю над проектом веб-API, и когда я запускаю его и пытаюсь использовать маршрут для контроллера, он не работает и вместо этого выдает ошибку 404. Почему веб-API игнорирует атрибут маршрута?
Я оставлю код ниже:

Код: Выделить всё

[ApiController]
[Route("api/[controller]")]

public class Controller3 : ControllerBase
{
private readonly IServiceContract3 _interest;
public Controller3(IServiceContract3 interest)
{
_interest = interest;
}

[HttpGet]
[Route("[action]")]
[Route("api/Interest/GetInterests")]
public IEnumerable GetEmployees()
{
return _interest.GetInterests();
}

[HttpPost]
[Route("[action]")]
[Route("api/Interest/AddInterest")]
public IActionResult AddInterest(Interest interest)
{
_interest.AddInterest(interest);
return Ok();
}

[HttpPost]
[Route("[action]")]
[Route("api/Interest/UpdateInterest")]
public IActionResult UpdateInterest(Interest interest)
{
_interest.UpdateInterest(interest);
return Ok();
}

[HttpDelete]
[Route("[action]")]
[Route("api/Interest/DeleteInterest")]
public IActionResult DeleteInterest(int id)
{
var existingInterest = _interest.GetInterest(id);
if (existingInterest != null)
{
_interest.DeleteInterest(existingInterest.Id);
return Ok();
}
return NotFound($"Employee Not Found with ID : {existingInterest.Id}");
}

[HttpGet]
[Route("GetInterest")]
public Interest GetInterest(int id)
{
return _interest.GetInterest(id);
}

}
И для моего Startup.cs

Код: Выделить всё

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

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContextPool(options =>
options.UseSqlServer(Configuration.GetConnectionString("DB")));
}

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

context.Database.Migrate();

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Как исправить маршрутизацию? Каждый раз, когда я пытаюсь сделать это в своем браузере, например https://localhost:44316/api/Interest/GetInterests, вместо этого я получаю ошибку 404. Почему это происходит?

Подробнее здесь: https://stackoverflow.com/questions/701 ... -attribute

Вернуться в «C#»