Attribute Route is not Mapping Correctly

I've got my Startup.cs set up like so:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace BotApiV2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore()
                .AddApiExplorer();

            services.AddRazorPages();
            services.AddSwaggerGen();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

and my controller is set up like this:

using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using BotApiV2.DataLayer;

namespace BotApiV2 { [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ValuesController))] [Route("api/values")] public class ValuesController : ControllerBase { [HttpGet] [Route("test")] public ActionResult Test() { var x = new BotDatabaseContext();

        var t = new Dictionary<string, string>
        {
            { "test1", "value1" },
            { "test2", "value2" },
            { "test3", "value3" }
        };
        return new JsonResult(t);
    }
}

}


Browsing to `/api/values/test` cannot be found however. Is there something set up wrong? I'm targeting .NET 5.0 (I've also tried .NET Core 3.1, same result).


Read more here: https://stackoverflow.com/questions/68463135/attribute-route-is-not-mapping-correctly

Content Attribution

This content was originally published by muttley91 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: