简体   繁体   English

带有 Web API 路由的 .NET Core 3.1 MVC 不起作用

[英].NET Core 3.1 MVC with Web API Routing not working

Having previously worked in .NET Framework, I'm now getting started with .NET Core.我以前在 .NET Framework 中工作过,现在开始使用 .NET Core。

I am building an application that is both MVC and Web API, and some of my routing is not working.我正在构建一个既是 MVC 又是 Web API 的应用程序,但我的一些路由不起作用。

Here is my controller code:这是我的控制器代码:

    [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));
        }
    }

And here is my startup.cs code:这是我的 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?}");
            });
        }
    }
  • https://localhost:5001/api/client works https://localhost:5001/api/client 有效
  • https://localhost:5001/api/client/test 404 error https://localhost:5001/api/client/test 404 错误
  • https://localhost:5001/api/client/2 404 error https://localhost:5001/api/client/2 404 错误
  • https://localhost:5001/api/client?id=2 404 error https://localhost:5001/api/client?id=2 404 错误

I have tried adding specific routes to my individual actions and that had no effect.我曾尝试将特定路线添加到我的个人操作中,但没有任何效果。

Thanks in advance.提前致谢。

You can not gain anything making route attribute like this for the controller您无法获得任何使控制器具有这样的路由属性的东西

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

You can remove any attribute routing from the controller with [ApiController] together or make it like this你可以和 [ApiController] 一起从控制器中删除任何属性路由,或者像这样

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

in this case you can use this variants在这种情况下,您可以使用此变体

        [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");
        }

and fix input并修复输入

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

but it is the same但它是一样的

  [HttpGet]
  public ActionResult input(int id)

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

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