Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WebAPI中,如何在不使用属性的情况下,使用两个get方法指定到一个控制器的两个独立路由?_C#_Asp.net_Asp.net Web Api_Routing_Asp.net Web Api Routing - Fatal编程技术网

C# 在WebAPI中,如何在不使用属性的情况下,使用两个get方法指定到一个控制器的两个独立路由?

C# 在WebAPI中,如何在不使用属性的情况下,使用两个get方法指定到一个控制器的两个独立路由?,c#,asp.net,asp.net-web-api,routing,asp.net-web-api-routing,C#,Asp.net,Asp.net Web Api,Routing,Asp.net Web Api Routing,我有一个帐户控制器,它有两个Get方法,一个获取所有帐户而不接受任何输入,另一个获取Id int并返回特定帐户 项目约束条件是,我们不能将id作为默认路由上的可选参数,而是需要获取一个帐户,并获取通过不同路由访问的所有帐户,并且我们不能使用属性路由。因此,我们的路线配置目前如下所示: config.Routes.MapHttpRoute("AccountWithId", "api/account/{id}", new { action = "Get", id = RouteParameter.O

我有一个帐户控制器,它有两个Get方法,一个获取所有帐户而不接受任何输入,另一个获取Id int并返回特定帐户

项目约束条件是,我们不能将id作为默认路由上的可选参数,而是需要获取一个帐户,并获取通过不同路由访问的所有帐户,并且我们不能使用属性路由。因此,我们的路线配置目前如下所示:

config.Routes.MapHttpRoute("AccountWithId", "api/account/{id}", new { action = "Get", id = RouteParameter.Optional }
                , new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), id = @"\d+" });

config.Routes.MapHttpRoute("Default", "api/{controller}");
public IHttpActionResult Get()
        {
            IList<AccountDto> users = m_service.Get();

            return Ok(users);
        }

        public IHttpActionResult Get(int accountId)
        {
            AccountDto user = m_service.Get(accountId);

            return Ok(user);
        }
AccountController上的两个Get方法如下所示:

config.Routes.MapHttpRoute("AccountWithId", "api/account/{id}", new { action = "Get", id = RouteParameter.Optional }
                , new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), id = @"\d+" });

config.Routes.MapHttpRoute("Default", "api/{controller}");
public IHttpActionResult Get()
        {
            IList<AccountDto> users = m_service.Get();

            return Ok(users);
        }

        public IHttpActionResult Get(int accountId)
        {
            AccountDto user = m_service.Get(accountId);

            return Ok(user);
        }

知道为什么吗?

我在这里看到了许多问题

您的第一条路由没有指定默认控制器,我很惊讶您在发送accountid时没有得到404

其次,如果id参数是可选的,则regex应该是\d*而不是\d+

第三,操作中的参数名称应该与路由参数匹配,因此accountId应该更改为id

为您的路由尝试以下方法:

config.Routes.MapHttpRoute("AccountWithId",
    "api/account/{id}",
    new { controller = "Account", action = "Get", id = RouteParameter.Optional },
    new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), id = @"\d*" }
);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
然后将您的操作签名更改为:

public IHttpActionResult Get()
{
    ...
}

public IHttpActionResult Get(int id)
{
    ...
}

如果accountId是可选的,则Get(int accountId)将被重载Get()隐藏。所以你不能让accountId成为可选的。很明显,当一个值是可选的时,它总是会到达同一个路由,因为如果提供或不提供它的可选,那么路由将始终是优先的,并且会被重载隐藏get@Win有道理,但没有RouteParameter。必选选项,它的失效方式与移除该参数的方式完全相同。我遵循了这个问题中的建议:很好地理解了参数名和正则表达式,谢谢。我应该补充一个问题,我确实尝试过指定帐户控制器,但它并没有解决问题。如果在默认路由上没有“/{id}”,那么就没有办法实现这一点吗?是的,如果要删除默认路由(defaultapi)上的{id}参数,它应该仍然适用于上面显示的情况。我已经测试了这个,它的路线正确,所以如果你仍然有问题,一定有一些信息我们丢失。