小编A.J*_*A.J的帖子

创建路由时出错

我尝试向我的 .NET Core 项目添加一个区域,但我总是看到该错误:

RouteCreationException:创建名称为“(我的区域名称)”的路线时出错

我的代码是:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapRoute(
            name: "custom",
            template: "{area:my area name}/{{controller=AdminHome}/{action=Index}/{id?}");
    });
}
Run Code Online (Sandbox Code Playgroud)

在配置服务中,我添加了以下代码:

public void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddRouting(); 

    //...
}
Run Code Online (Sandbox Code Playgroud)

在控制器中我添加了:

[Area("My Area Name")]
public class AdminHomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误是:

RouteCreationException:创建名称为“custom”且模板为“{area:area name}/{{controller=Home}/{action=Index}/{id?}”的路由时出错。\r\n Microsoft.AspNetCore.Routing.RouteBase..ctor(string template, string name, IInlineConstraintResolver constraintResolver, …

c# asp.net-mvc-routing asp.net-mvc-areas asp.net-core

8
推荐指数
3
解决办法
1万
查看次数