如何使用不同的参数类型相同的路线? [英] How to use the same route with different parameter types?
问题描述
我对此采取不同的参数类型两个不同的动作的API控制器。
I have an Api controller with two different actions that take different parameter types.
// GET: users/sample%40email.com
[Route("users/{emailAddress}")]
public IHttpActionResult GetUser(string emailAddress)
// GET: users/1D8F6B90-9BD9-4CDD-BABB-372242AD9960
[Route("users/{reference}")]
public IHttpActionResult GetUserByReference(Guid reference)
问题是多个动作中找到匹配的,当我做出任何请求。综观其他答案我想我需要设置航线的WebApiConfig像这样...
Problem is multiple actions are found matching when I make a request to either. Looking at other answers I thought I needed to setup routes in the WebApiConfig like so...
config.Routes.MapHttpRoute(
name: "apiEmail",
routeTemplate: "api/{controller}/{action}/{email}"
);
config.Routes.MapHttpRoute(
name: "apiReference",
routeTemplate: "api/{controller}/{action}/{reference}"
);
什么是我需要做的,使每一个动作是基于参数类型我通过叫什么名字?
What do I need to do so that each action is called based on the parameter type I pass in?
我很新的Web.Api任何额外的解释文本将AP preciated。
I'm very new to Web.Api any additional explanation text would be appreciated.
推荐答案
您做像下面方法声明启用路由属性:
//申报法GUID 1日
You do like below method declaration with attribute routing enabled: //declare method with guid 1st
// GET: users/1D8F6B90-9BD9-4CDD-BABB-372242AD9960
[Route("users/{reference:guid}")]
public IHttpActionResult GetUserByReference(Guid reference)
和其他申报方式如下图所示。
and declare other method like below
// GET: users/sample%40email.com
[Route("users/{emailAddress}")]
public IHttpActionResult GetUser(string emailAddress)
请让我知道,这是对你的工作?
Please let me know, is this work for you ?
这篇关于如何使用不同的参数类型相同的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!