简体   繁体   English

ASP.NET 核心 Web API 常规路由

[英]ASP.NET Core Web API Conventional routing

In ASP.NET Core Web API I am using Attribute routing and I need to move it to conventional routing.在 ASP.NET 核心 Web API 我正在使用属性路由,我需要将其移动到常规路由。

 [ApiController]
 public class HomeController : Controller
 {
    [Route("GetHome")]
    [AllowAnonymous]
    [HttpGet]
    public string Getdeatils([FromBody] myclass cs, string system)
    {
    }
 }

With Attribute routing the URL localhost/GetHome?system=abc works and triggers Getdeatils method.使用属性路由 URL localhost/GetHome?system=abc工作并触发Getdeatils方法。

I am trying to move same URL pattern to startup, but I am unable to achieve it.I have removed [ApiController] from controller class I have tried below code in startup.我正在尝试将相同的 URL 模式移动到启动,但我无法实现它。我已经从 controller class 中删除了[ApiController]我在启动时尝试了以下代码。 But it is not working.但它不起作用。

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

URL: localhost/Home/Getdeatils?system=abc - This works fine URL: localhost/Home/Getdeatils?system=abc - 这很好用

URL: localhost/GetHome?system=abc - This is not working. URL: localhost/GetHome?system=abc - 这不起作用。

How this can be achieved without changing the URL format.如何在不更改 URL 格式的情况下实现这一点。

Please try with this code:请尝试使用此代码:

app.UseEndPoints(endpoints =>
{
  endpoints.MapControllerRoute(name: "mycustom",
           pattern: "GetHome",
           defaults: new {controller="Home", action="GetDetails"});
  endpoints.MapControllerRoute(name: "default",
           pattern: "{controller=Home}/{action=Index}/{system?}");
}

Please let me know if it works as expected.请让我知道它是否按预期工作。

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

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