简体   繁体   English

ASP.NET Core 3.0 Endpoint Routing 不工作并且找不到 404

[英]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 .我的端点路由在我的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我在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:我的Ping controller看起来像:

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

navigating to http://localhost/ /api/v1.0/Ping returns 404 page not found.导航到http://localhost/ /api/v1.0/Ping 返回 404 页面未找到。

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.我还看到 MS 建议为 Web APIs 进行属性路由,但想弄清楚为什么这首先不起作用。

@rfcdejong was correct, it was missing {action} token. @rfcdejong 是正确的,它缺少{action}标记。 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按照这个在 .Net Core 3.1 中添加额外的路由

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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