简体   繁体   中英

ASP.NET Core 3.0 Endpoint Routing not working and getting 404 not found

My endpoint routing is not working in my asp.net core 3.0 Api . I have seen similar questions but I am still not sure what is missing here.

I have the following in Startup.cs

   {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.AddControllers()
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.Formatting = Formatting.Indented;
                    options.SerializerSettings.Converters.Add(
                        new Newtonsoft.Json.Converters.StringEnumConverter());
                });        
        }


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

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "test",
                    "api/v1.0/{controller}/{id?}");
            });
        }            
}

My Ping controller looks like:

 public class PingController : ControllerBase
    {
        public IActionResult Get()
        {
            return Ok(true);
        }
    } 

navigating to http://localhost/ /api/v1.0/Ping returns 404 page not found.

What am I missing here ? I also saw that MS suggests attribute routing for Web Apis but wanted to figure out why this is not working in the first place.

@rfcdejong was correct, it was missing {action} token. The following correction fixed the problem.

endpoints.MapControllerRoute( "test", "api/v1.0/{controller}/{action=Get}/{id?}");

Follow this to add additional routing in .Net Core 3.1

  endpoints.MapControllerRoute(
                      name: "prod", pattern: "product/item", new { controller="Product", action="Index"});

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