简体   繁体   中英

Microsoft.AspNetCore.Routing.RouteCreationException: 'An error occurred while creating the route with name 'default' and template

I am a newbie to the Asp.Net core, and I am just creating a new Asp.Net Core 2 application, but getting as error as preceding.

Microsoft.AspNetCore.Routing.RouteCreationException: 'An error occurred while creating the route with name 'default' and template '{controller=Home}/{action=Index}/{id=?}'.'

Below is my Configure function code.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

The screenshot of the error is given below.

在此处输入图片说明

Can anyone please help me resolve this issue?

Huh, it was my mistake. I added an extra character {id=?} in the template, instead of {id?} . So my working code is looking as preceding.

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

I read this information from here , just sharing as it may help someone.

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