如何在.net Core 3中设置隐式路由

如何隐式定义路由?例如,我有一个名称为TodoController的控制器,其中有一些操作,例如“添加”,“获取”,“保存”。而且,我不想为每个控制器都指定“路由属性”。

这是我的代码:

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();
        });
dqsj09021013 回答:如何在.net Core 3中设置隐式路由

我的研究结果。

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

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属性,但是没有他就没有变体。

本文链接:https://www.f2er.com/3127547.html

大家都在问