.NET Core 3.1 MVC with Web API 路由不工作

.NET Core 3.1 MVC with Web API Routing not working

我之前在 .NET Framework 工作过,现在开始使用 .NET Core。

我正在构建一个既是 MVC 又是 Web 的应用程序 API,但我的某些路由无法正常工作。

这是我的控制器代码:

    [Route("api/[controller]")]
    [ApiController]
    public class ClientController : ControllerBase
    {
        private readonly ApplicationDbContext db;
        public ClientController(ApplicationDbContext context)
        {
            db = context;
        }

        [HttpGet]
        public ActionResult Get()
        {
            return Ok("get works");
        }

        [HttpGet]
        public ActionResult Test()
        {
            return Ok("test not working");
        }

        [HttpGet]
        [Route("api/client/input")]
        public ActionResult input(int id)
        {
            return Ok(string.Format("Your id: {0}", id));
        }
    }

这是我的 startup.cs 代码:

    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.AddDbContext<ApplicationDbContext>(opts => opts.UseSqlServer(Configuration.GetConnectionString("sqlConnection")));
            services.AddMvc();
        }

        // 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("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

我已经尝试将特定路线添加到我的个人操作中,但没有效果。

提前致谢。

你无法获得任何为控制器制作这样的路由属性

  [Route("api/[controller]")]
    [ApiController]
    public class ClientController : ControllerBase

您可以使用 [ApiController] 一起从控制器中删除任何属性路由,或者像这样

   [Route("api/[controller]/[action]")]
    [ApiController]
    public class ClientController : ControllerBase

在这种情况下,您可以使用此变体

        [HttpGet]
        public ActionResult Get() //api/client/get
        //or
         [HttpGet("GetAll")]
        public ActionResult Get() //api/client/getall

        [HttpGet]
        public ActionResult Test() //api/client/test
        {
            return Ok("test not working");
        }

并修复输入

        [HttpGet]
        [Route("~/api/client/input/{id}")] //api/client/input/4
        public ActionResult input(int id)

不过是一样的

  [HttpGet]
  public ActionResult input(int id)