简体   繁体   English

asp net core 路由多个参数

[英]Asp net core routing multiple parameters

I am trying to route asp.net core application.我正在尝试路由 asp.net 核心应用程序。

I have having this scenario:我有这个场景:

[Route("/{user}")]
public IActionResult FirstAction(string user)
{
    // Code
}

[Route("/{user}/{project}")]
public IActionResult SecondAction(string user, string project)
{
    // Code 2
}

Problem i am having is that when user goes to link /something it fires both methods ( // Code 1 and // Code 2 ) but i want only first one to fire.我遇到的问题是,当用户去链接/something它会触发两种方法( // Code 1// Code 2 ),但我只想触发第一个。

How can i achieve this?我怎样才能做到这一点?

Use a better REST API routing使用更好的 REST API 路由

[Route("/users/{user}")]

[Route("/users/{user}/projects/{project}")]

Are you sure that both actions are fired?你确定这两个动作都被触发了吗? Which version of ASP NET Core are you using?您使用的是哪个版本的 ASP NET Core?

I tested the following:我测试了以下内容:

    [HttpGet("/{user}")]
    public ActionResult<string> Test1(string user)
    {
        return "User " + user;
    }

    [HttpGet("/{user}/{project}")]
    public ActionResult<string> Test2(string user, string project)
    {
        return "User " + user + " Project " + project;
    }

The routes where explicit.明确的路线。 If there was only one string Test1 was routed.如果只有一个字符串 Test1 被路由。 If there were two strings Test2 was routed.如果有两个字符串 Test2 被路由。


Furthermore, please note the following: If you put a leading / into the route any prefixes will be ignored.此外,请注意以下事项: 如果您将 / 放在路由中,任何前缀都将被忽略。

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

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