find and solve || findandsolve.com
Please wait.....

An error occurred while creating a route In a route parameter

An error occurred while creating a route In a route parameter


           routes.MapRoute(
                        name: "SourceCodeDtetails",
                        template: "source-code{/{titleUrl}}",
                        defaults: new { Controller = "Utilities", Action = "SoureCodeDetail" });An error occurred while starting the application.

This is my code in Startup.cs.When I have run the application the error has been occured like as given below.

RoutePatternException: In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.

Microsoft.AspNetCore.Routing.Patterns.RoutePatternParser.Parse(string pattern)

ArgumentException: In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.

Parameter name: routeTemplate

Microsoft.AspNetCore.Routing.Template.TemplateParser.Parse(string routeTemplate)

Microsoft.AspNetCore.Routing.RouteBase..ctor(string template, string name, IInlineConstraintResolver constraintResolver, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens)

Microsoft.AspNetCore.Builder.MapRouteRouteBuilderExtensions.MapRoute(IRouteBuilder routeBuilder, string name, string template, object defaults, object constraints, object dataTokens)

Microsoft.AspNetCore.Builder.MapRouteRouteBuilderExtensions.MapRoute(IRouteBuilder routeBuilder, string name, string template, object defaults)

Artical.UI.Startup+<>c.<Configure>b__7_0(IRouteBuilder routes) in Startup.cs+

                routes.MapRoute(

Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action<IRouteBuilder> configureRoutes)

Artical.UI.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs

+

            app.UseMvc(routes =>

Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)

Microsoft.AspNetCore.Mvc.Internal.MiddlewareFilterBuilderStartupFilter+<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)

Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)

Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)

Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)

Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()


Answer


Option 1.

Pease add "/" befor {titleUrl} like as

           routes.MapRoute(
                        name: "SourceCodeDtetails",
                        template: "source-code/{titleUrl}",
                        defaults: new { Controller = "Utilities", Action = "SoureCodeDetail" });


Option 2.

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/hello/{name:alpha}", async context =>
    {
        var name = context.Request.RouteValues["name"];
        await context.Response.WriteAsync($"Hello {name}!");
    });
});

Option 3.

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

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



Selena  Kandakar

Selena Kandakar

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response