【问题标题】:ASP.NET Core 3.0 Endpoint Routing doesn't work for default routeASP.NET Core 3.0 端点路由不适用于默认路由
【发布时间】:2021-11-16 10:15:12
【问题描述】:

我已根据 this page 的指导方针将现有 API 项目从 2.2 迁移到 3.0

因此我删除了:

app.UseMvc(options =>
{
    options.MapRoute("Default", "{controller=Default}/{action=Index}/{id?}");
});

并插入:

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

但不会绑定控制器和操作。我调用的任何 API 得到的都是 404。

我应该如何调试它,我在这里错过了什么?

更新:Startup.cs 文件位于另一个程序集中。我们在许多项目中重复使用集中式 Startup.cs 文件。

【问题讨论】:

  • 不,它们没有任何属性。我们只有一个全局默认路由规则。我们在其他任何地方都没有创建任何其他路由,无论是在操作级别还是控制器级别。
  • 可能为时已晚,但由于没有可接受的答案:您在配置中是否有选项options.EnableEndpointRouting = false?因为这告诉框架继续使用“旧”路由,而不是端点路由。

标签: c# asp.net-core-3.0


【解决方案1】:

来自Attribute routing vs conventional routing

通常为为浏览器提供 HTML 页面的控制器使用传统路由,为为 REST API 提供服务的控制器使用属性路由。

来自Build web APIs with ASP.NET Core: Attribute routing requirement

[ApiController] 属性使属性路由成为一项要求。例如:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

无法通过 Startup.Configure 中的 UseEndpointsUseMvcUseMvcWithDefaultRoute 定义的常规路由访问操作。

如果你想在 web api 上使用常规路由,你需要在 web api 上禁用属性路由。

启动:

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    }

    // 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();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

Web api 控制器:

 //[Route("api/[controller]")]
//[ApiController]
public class DefaultController : ControllerBase
{
    public  ActionResult<string> Index()
    {
        return "value";
    }

    //[HttpGet("{id}")]
    public ActionResult<int> GetById(int id)
    {
        return id;
    }
}

这可以由http://localhost:44888/default/getbyid/123提出请求

【讨论】:

  • 我们的操作除了HttpGetHttpPost 之外没有任何属性。并且在我们的控制器上根本没有属性。
  • @mohammadrostamisiahgelimo ,请出示你的Startup.cs .
  • 我不得不用旧模式改回UseMvc
【解决方案2】:

我可以推荐我的解决方案。

像这样创建您的 CUSTOM 基本控制器。

    [Route("api/[controller]/[action]/{id?}")]
    [ApiController]
    public class CustomBaseController : ControllerBase
    {
    }

并使用 CustomBaseController

 public class TestController : CustomBaseController
    {
        public IActionResult Test()
        {
            return Ok($"Test {DateTime.UtcNow}");
        }
    }

Rout` api/Test/test

【讨论】:

    【解决方案3】:

    你应该试试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 2017-07-08
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多