10

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
A.J
  • 139
  • 1
  • 1
  • 10
  • i see some links about that : -> https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing AND https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas ... and etc... – A.J Mar 28 '18 at 12:27

4 Answers4

14

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • .net core has come to ease the software development of programmers . but on the contrary was the case after 6 years programming with MVC now I can't add an area to my project . that's rely funny – A.J Mar 28 '18 at 14:06
  • I can't believe I needed this answer to solve this. It is 9pm. Time to quit for today. Thanks anyway! I was missing a } as well, but on my first parameter actually in my second route attribute – Yeronimo May 06 '20 at 16:07
9

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 missing the closing "}" after userId, which caused this issue.

WORKING

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

Hope it helps others :)

Manthan Devani
  • 179
  • 2
  • 4
5

Lot of times you make several changes to many files and then you are searching in the Statup.cs in .net core only to end up realizing you messed up an attribute on a new web api method like this

[HttpGet("{id:int")]  
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
  • 1
    I read about half of the error. I copied and pasted it to google. Read about half of the first result before clicking it - which led me here... scrolled a bit, barely caught this answer before reading about half of it.... then went and checked the thing the example is showing in my own code and..........................fixed the issue immediately. #DevLife – default_noob_network Apr 29 '22 at 14:38
2
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!

Adriano
  • 3,788
  • 5
  • 32
  • 53
A.J
  • 139
  • 1
  • 1
  • 10