【问题标题】:How to set implicit routing in .net core 3如何在.net core 3中设置隐式路由
【发布时间】:2019-11-10 21:11:21
【问题描述】:

如何隐式定义路由?例如,我有一个名为 TodoController 的控制器,其中包含一些操作,例如添加、获取、保存..而且我不希望为每个操作指定 Route 属性。

这是我的代码:

public class TodoController : ControllerBase
{
    [HttpPost]
    public IActionResult New()
    {
        return Ok();
    }

    [HttpGet]
    public IActionResult Prova()
    {
        return Ok();
    }

这是我的 startup.cs

         if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

【问题讨论】:

    标签: .net-core routes


    【解决方案1】:

    我的研究结果。

    您可以使用两种类型的路由。

    1.

    services.AddMvc(option => option.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
    
    
    
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" }
        );
    
    });
    

    `

    2.

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

    两种变体都与 Rout 属性一起使用,但没有他就没有变体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 2017-07-10
      • 2020-04-17
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      相关资源
      最近更新 更多