简体   繁体   中英

An error occurred while creating a route

I try to add an area to my .NET Core project, but always I see that error:

RouteCreationException: An error occurred while creating the route with name '(My Area Name)'

My Code is :

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?}");
    });
}

And in configure services I add this code:

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

    services.AddRouting(); 

    //...
}

And in controller I added:

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

The error is :

RouteCreationException: An error occurred while creating the route with name 'custom' and template '{area:area name}/{{controller=Home}/{action=Index}/{id?}'. \\r\\n Microsoft.AspNetCore.Routing.RouteBase..ctor(string template, string name, IInlineConstraintResolver constraintResolver, RouteValueDictionary defaults, IDictionary constraints, RouteValueDictionary dataTokens) \\r\\n ArgumentException: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character. \\r\\n Parameter name: routeTemplate

As stated in the error message, you have a stray { in the route template that is making it invalid

template: "{area:my area name}/{{controller=AdminHome}/{action=Index}/{id?}");
                               ^
                               |
                             here

You also need to rearrange the order of routes to avoid route conflicts.

app.UseMvc(routes => {
    routes.MapRoute(
        name: "custom",
        template: "{area:my area name}/{controller=AdminHome}/{action=Index}/{id?}");

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

Reference Areas in ASP.NET Core

I am using .Net Core 3.1 Web API project, the problem was in the controller where we specify the route at the the top of the controller, Below is snippet of what was wrong and then what was correct :

ERROR

[Route("api/users/{userId/photos")]

I was , which caused this issue. ,这导致了这个问题。

WORKING

[Route("api/users/{userId /photos")] /photos")]

Hope it helps others :)

routes.MapRoute(
    name: "default",
    template: "{controller}/{action}/{id?}",
    defaults: new { controller = "Home", action = "Index", id = "id?" } 
);
routes.MapRoute(
    name: "persianredditacp",
    template: "{area}/{controller}/{action}/{id?}"
);  

THAT worked!

很多时候,您对许多文件进行了多次更改,然后您在 .net 核心中的 Statup.cs 中进行搜索,结果却发现您弄乱了像这样的新 Web api 方法上的属性

[HttpGet("{id:int")]  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM