【问题标题】:Controller with HttpGet(...) gives error 404带有 HttpGet(...) 的控制器给出错误 404
【发布时间】: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


【解决方案1】:

路由是:localhost:5001/brand/add-brand

因此,您希望该操作可以在来自 BrandController 的 /brand 中调用。但这不是属性路由的工作原理。

默认情况下,ASP.NET Core 对控制器使用常规路由。它使用的默认模板是/{controller}/{action}。这就是为什么如果您不使用基于属性的路由,访问/brand/addbrand 会起作用。

但是,一旦您通过属性([Route(…)] 或特定于方法的属性,如[HttpGet(…)])指定路由,您基本上就禁用了基于约定的路由。因此,该操作的路线将由路线属性确定。由于您只有一个 [HttpGet("/add-brand")],因此您的操作的实际路径将是 https://localhost:5001/add-brand,而没有额外的控制器路径段。

如果你喜欢使用[HttpGet("/{controller}/add-brand")],你可以把它放回去。另一种方法是在控制器上添加一个单独的[Route("{controller}")],然后将所有操作路由连接到此。请注意,这将禁用 所有 路由的常规路由,需要您为每个操作指定它。

【讨论】:

    【解决方案2】:

    Camilo 是正确的,路由字符串中不需要前导 /,/brand/ 是控制器隐含的。

    public class BrandController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    
        [HttpGet("add-brand")]
        public async Task<IActionResult> AddBrand()
        {
            return View();
        }
    }
    

    【讨论】:

    • 我将 [HttpGet("/add-brand")] 替换为 [HttpGet("add-brand")] ,但仍然出现错误 404
    • 所以您正在导航到“hostname/Brand/add-brand”?
    • 戳上面的答案很好地解释了它
    【解决方案3】:

    当您尝试 /brand/addbrand(不带 [HttpGet("/add-brand")])时:

    MVC 使用路由模板“{controller}/{action}” 它将brand 与 BrandController 匹配,并将 addbrand 与您的操作名称匹配

    我很好奇为什么 [HttpGet("/add-brand")] 中需要 '/'。如果您尝试 [HttpGet("add-brand")] 它应该可以正常工作。

    【讨论】:

    • 我将 [HttpGet("/add-brand")] 替换为 [HttpGet("add-brand")] ,但仍然出现错误 404
    猜你喜欢
    • 2014-06-03
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多