You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC点击创建按钮出现404错误的求助

排查ASP.NET Core MVC中Create路由404错误

问题场景

当前使用ASP.NET Core MVC项目,访问https://localhost:4400/advertisers正常,该路由对应Advertisers控制器的Index方法(Get请求)。在Index视图中添加了“创建广告主”按钮,点击后浏览器跳转到https://localhost:4400/advertisers/create,但抛出404错误:

This localhost page can’t be found No webpage was found for the web address: https://localhost:4400/advertisers/create HTTP ERROR 404

相关代码

Advertisers控制器Index方法

public async Task<IActionResult> Index()
{
    var advertiserList = await _advertisersService.GetActiveAsync();
    return View(advertiserList);
}

Index视图中的创建按钮

<a asp-action="Create" asp-controller="Advertisers"><button type="button" class="btn btn-primary waves-effect waves-primary"><i class="bx bx-plus me-1"></i> Add Advertiser</button></a>

Advertisers控制器Create方法

[HttpGet]
public IActionResult Create()
{
    return View();
}

Startup.cs配置

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.AddRouting(options => options.LowercaseUrls = true);

    services
        .AddControllersWithViews()
        .AddSessionStateTempDataProvider()
        .AddRazorRuntimeCompilation();
}

[UsedImplicitly]
public void Configure(
    IApplicationBuilder app,
    IWebHostEnvironment env,
    IApiVersionDescriptionProvider provider,
    ApplicationDbContext db
)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");

        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    db.Database.Migrate();
    app.UseSession();
    app.UseSwagger();

    app.UseSwaggerUI(
        options =>
        {
            foreach (var groupName in provider.ApiVersionDescriptions.Select(x => x.GroupName))
                options.SwaggerEndpoint($"/swagger/{groupName}/swagger.json", $"{groupName}");
        }
    );
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(
        endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
        }
    );
}

排查方向

  • 确认控制器类命名规范:MVC控制器类必须以Controller后缀结尾,检查你的控制器类是否是AdvertisersController,如果类名缺少Controller后缀,路由系统无法识别,会导致404。
  • 检查Create视图是否存在:Create方法返回View()时,默认会查找Views/Advertisers/Create.cshtml文件,如果该视图不存在,可能导致请求失败。
  • 排查路由属性冲突:检查Advertisers控制器或Create方法是否添加了[Route]属性,这类属性会覆盖默认路由规则,导致advertisers/create无法匹配到对应的Action。
  • 清理并重新构建项目:编译缓存可能引发异常,尝试清理项目的bin和obj目录,重新生成解决方案后再测试。
  • 验证路由配置:虽然使用了默认路由规则{controller=Home}/{action=Index}/{id?},但可以临时添加显式路由映射测试:
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        // 添加临时测试路由
        endpoints.MapControllerRoute(
            name: "advertiser-create",
            pattern: "advertisers/create",
            defaults: new { controller = "Advertisers", action = "Create" });
    });
    
    如果添加后能正常访问,说明默认路由可能存在未发现的冲突。

内容的提问来源于stack exchange,提问作者Jesus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:30:45