Web 中两种不同方法的相同方法签名 api
Same method signature for two diff methods in web api
它可能有重复但我没有找到正确的解决方案,
我的网站api,
public class SampleController : ApiController
{
public string Get(int id)
{
return "value";
}
public string hello(int id)
{
return "value";
}
}
我的网站api配置,
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的问题是
当我调用 http://localhost:1234/api/Sample/5 时,它正在调用 Get(int id) 但我如何调用方法 2,即 hello(int id) ?需要更改哪些内容以及处理此类情况的最佳方法是什么??
我不确定我是否答对了你的问题,但如果我答对了:
您应该以其他方式而不是在其名称中指定函数的路由。根据我对该主题的一点经验,我就是这样做的:
[HttpGet]
[Route("SystemInfo")] // That's the name of the route you will call
public IHttpActionResult SystemInfo()
{
return Ok();
}
考虑检查 this。
所以,考虑到你的问题,应该是这样的:
[Route("Get")]
public string Get(int id)
{
return "value";
}
[Route("hello")]
public string hello(int id)
{
return "value";
}
TLDR:
如果您想在您的网站中引用个别操作 API 然后将您的路由更改为:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后您可以像这样访问您的操作:localhost/api/{controller}/{action}/。看here for further information, especially "Routing by Action Name".
原:
您似乎期望与 MVC 控制器相同的行为。 MVC-Controller 的 Standard-Routing 是这样的:
routeTemplate: "{controller}/{action}/{id}"
这对应于控制器的名称、要使用的方法和某种形式的输入。 ApiControllers 路由不同:
routeTemplate: "staticPart/{controller}/{id}"
如您所见,只有对单个控制器和输入的引用,以及通常类似于 /api/
的 "staticPart"
想法是您使用 RESTful 方法,将方法与不同类型的 http 方法(例如 DELETE、GET、POST、PUSH 和 PUT)连接起来
您示例中的 Get 方法是一个特殊方法,因为您通过名称 "Get" 告诉编译器此方法对应于 HTTP-GET。
所以开始回答您的问题:要么将您的路由更改为 MVC-Controller 的路由。以便您在请求中引用单独的操作或使用不同的 HTTP-Methods。或者您单独设置路线,如
所示
您可以在 Web API 路由上找到官方概述 here 您可以在那里找到所有可能性的示例。
它可能有重复但我没有找到正确的解决方案,
我的网站api,
public class SampleController : ApiController
{
public string Get(int id)
{
return "value";
}
public string hello(int id)
{
return "value";
}
}
我的网站api配置,
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的问题是
当我调用 http://localhost:1234/api/Sample/5 时,它正在调用 Get(int id) 但我如何调用方法 2,即 hello(int id) ?需要更改哪些内容以及处理此类情况的最佳方法是什么??
我不确定我是否答对了你的问题,但如果我答对了:
您应该以其他方式而不是在其名称中指定函数的路由。根据我对该主题的一点经验,我就是这样做的:
[HttpGet]
[Route("SystemInfo")] // That's the name of the route you will call
public IHttpActionResult SystemInfo()
{
return Ok();
}
考虑检查 this。
所以,考虑到你的问题,应该是这样的:
[Route("Get")]
public string Get(int id)
{
return "value";
}
[Route("hello")]
public string hello(int id)
{
return "value";
}
TLDR:
如果您想在您的网站中引用个别操作 API 然后将您的路由更改为:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后您可以像这样访问您的操作:localhost/api/{controller}/{action}/。看here for further information, especially "Routing by Action Name".
原:
您似乎期望与 MVC 控制器相同的行为。 MVC-Controller 的 Standard-Routing 是这样的:
routeTemplate: "{controller}/{action}/{id}"
这对应于控制器的名称、要使用的方法和某种形式的输入。 ApiControllers 路由不同:
routeTemplate: "staticPart/{controller}/{id}"
如您所见,只有对单个控制器和输入的引用,以及通常类似于 /api/
的 "staticPart"想法是您使用 RESTful 方法,将方法与不同类型的 http 方法(例如 DELETE、GET、POST、PUSH 和 PUT)连接起来
您示例中的 Get 方法是一个特殊方法,因为您通过名称 "Get" 告诉编译器此方法对应于 HTTP-GET。
所以开始回答您的问题:要么将您的路由更改为 MVC-Controller 的路由。以便您在请求中引用单独的操作或使用不同的 HTTP-Methods。或者您单独设置路线,如
您可以在 Web API 路由上找到官方概述 here 您可以在那里找到所有可能性的示例。