【发布时间】:2020-11-07 19:28:02
【问题描述】:
我很难理解为什么我在使用 [HttpGet("/add-brand")] 但没有 [HttpGet("/add-brand")] 时会出现错误 404 错误?
这是我的控制器:
public class BrandController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet("/add-brand")]
public async Task<IActionResult> AddBrand()
{
return View();
}
}
如果路径是 /brand/addbrand(没有 [HttpGet("/add-brand")]),它可以工作,但我想改用 [HttpGet("/add-brand")]。
我做错了什么?
编辑:这是我的 Startup.cs 配置方法:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
【问题讨论】:
-
如果你使用
/route,你会删除控制器的路由。你要使用的是[HttpGet("add-brand")]。 -
我将 [HttpGet("/add-brand")] 替换为 [HttpGet("add-brand")] 仍然得到错误 404
-
你打电话的路线是什么?以及你的路由在启动时是如何配置的?
-
我在编辑中添加了我的 Startup.cs 配置方法。路线是:localhost:5001/brand/add-brand
标签: c# asp.net-core asp.net-core-mvc