我正在處理一個 Web API 專案,當我運行它并嘗試將路由用于控制器時,它不起作用,而是向我拋出 404 錯誤。為什么 Web 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<Interest> 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<DatabaseContext>(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 錯誤。為什么會這樣?
uj5u.com熱心網友回復:
通常 webapi 的控制器定義如下:
[ApiController]
[Route("api/[controller]")]
public sealed class InterestController : ControllerBase
{
private readonly IServiceContract3 _interest;
public InterestController(IServiceContract3 interest)
{
_interest = interest;
}
[HttpGet, Route("GetInterests")]
public IActionResult GetEmployees()
{
// this is located at: GET api/Interest/GetInterests
return Ok(_interest.GetInterests());
}
[HttpPost, Route("AddInterest")]
public IActionResult AddInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/AddInterest
}
[HttpPost, Route("UpdateInterest")]
public IActionResult UpdateInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/UpdateInterest
}
[HttpDelete, Route("DeleteInterest/{id}")]
public IActionResult DeleteInterest([FromRoute] int id)
{
// this is located at: DELETE api/Interest/DeleteInterest/{id}
}
[HttpGet, Route("GetInterest/{id}")]
public IActionResult GetInterest([FromRoute] int id)
{
// this is located at: GET api/Interest/GetInterest/{id}
return Ok(_interest.GetInterest(id));
}
}
首先,您要為控制器命名一個相關名稱,該名稱將貫穿控制器的其余部分。所以,這這種情況下,我換Controller3到InterestController。由于控制器的路由是"/api/[Controller]",它將轉換為"api/Interest"。
接下來,洗掉[Action]屬性。在這種情況下您不需要它們。
然后,確保您的路線正確。如果您要傳遞 ID,請在路由中使用{id}和設定[FromRoute]屬性來定義您的 ID,以便清楚起見。此外,用于[FromBody]定義將來自正文的引數。其中很多是“默認”(意味著您不需要添加它們),但它有助于清晰。
最后,如果您正在使用該IActionResult模式,請通過控制器堅持使用該模式。不要在端點之間混合/匹配回傳型別,因為這會導致維護混亂。
最后一件事:您的控制器端點名稱有點多余。例如,你可以這樣做:
[HttpGet, Route("")]
public IActionResult GetEmployees()
{
// this is located at: GET api/Interest
return Ok(_interest.GetInterests());
}
[HttpPut, Route("")]
public IActionResult AddInterest([FromBody] Interest interest)
{
// this is located at: PUT api/Interest/
}
[HttpPost, Route("")]
public IActionResult UpdateInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/
}
[HttpDelete, Route("{id}")]
public IActionResult DeleteInterest([FromRoute] int id)
{
// this is located at: DELETE api/Interest/{id}
}
[HttpGet, Route("{id}")]
public IActionResult GetInterest([FromRoute] int id)
{
// this is located at: GET api/Interest/{id}
return Ok(_interest.GetInterest(id));
}
即:使用“HTTP 動詞”來隔離操作。
uj5u.com熱心網友回復:
您必須通過使 api 成為根來修復您的路線。將“api”替換為“~/api”。恕我直言,您應該從動作中洗掉 [action] 并將其添加到控制器中。還要修復控制器名稱
[ApiController]
[Route("~/api/[controller]/[action]")]
public class InterestController : ControllerBase
[HttpGet("~/api/Interest/GetInterests")]
public IEnumerable<Interest> GetEmployees()
....
[HttpPost("~/api/Interest/AddInterest")]
public IActionResult AddInterest(Interest interest)
...
[HttpPost("~/api/Interest/UpdateInterest")]
public IActionResult UpdateInterest(Interest interest)
...
[HttpDelete("~/api/Interest/DeleteInterest/{id}")]
public IActionResult DeleteInterest(int id)
....
[HttpGet("~/api/Interest/GetInterest/{id}")]
public Interest GetInterest(int id)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367850.html
