Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 声明多个Get语句的Api控制器_C#_Asp.net Mvc 4_Asp.net Web Api_Asp.net Routing - Fatal编程技术网

C# 声明多个Get语句的Api控制器

C# 声明多个Get语句的Api控制器,c#,asp.net-mvc-4,asp.net-web-api,asp.net-routing,C#,Asp.net Mvc 4,Asp.net Web Api,Asp.net Routing,在MVC4中使用新的Api控制器,我发现了一个问题。如果我有以下方法: public IEnumberable GetAll() public IEnumberable GetSpecific(int i) 这会奏效的。但是,如果我想检索不同类型的不同数据,它默认为GetAll方法,即使$.getJSON设置为getallingers方法: public IEnumberable GetAllIntergers() (错误的命名约定) 我能做到这一点吗 在Web API控制器中只能有一个GetA

在MVC4中使用新的Api控制器,我发现了一个问题。如果我有以下方法:

public IEnumberable GetAll()

public IEnumberable GetSpecific(int i)

这会奏效的。但是,如果我想检索不同类型的不同数据,它默认为
GetAll
方法,即使
$.getJSON
设置为
getallingers
方法:

public IEnumberable GetAllIntergers()

(错误的命名约定)

我能做到这一点吗

在Web API控制器中只能有一个
GetAll
方法吗

我认为更容易想象我想要实现的目标。下面是一段代码片段,用一个
ApiController
来展示我希望能够做什么:

public IEnumerable<string> GetClients()
{ // Get data
}

public IEnumerable<string> GetClient(int id)
{ // Get data
}

public IEnumerable<string> GetStaffMember(int id)
{ // Get data
}

public IEnumerable<string> GetStaffMembers()
{ // Get data
}
public IEnumerable GetClients()
{//获取数据
}
公共IEnumerable GetClient(int id)
{//获取数据
}
公共IEnumerable GetStaffMember(int id)
{//获取数据
}
公共IEnumerable GetStaffMembers()
{//获取数据
}

这都在路由中。默认Web API路由如下所示:

config.Routes.MapHttpRoute( 
    name: "API Default", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
);
对于默认路由模板,Web API使用HTTP方法选择操作。结果,它将不带参数的GET请求映射到它能找到的第一个
GetAll
。要解决此问题,您需要定义包含操作名称的路由:

config.Routes.MapHttpRoute( 
   name: "ActionApi", 
   routeTemplate: "api/{controller}/{action}/{id}", 
   defaults: new { id = RouteParameter.Optional } 
);
之后,您可以使用以下URL启动请求:

  • api/YourApicController/GetClient
  • api/YourApicController/GetStaffMembers
这样,您可以在控制器中拥有多个
GetAll

这里更重要的一点是,对于这种类型的路由,必须使用属性来指定允许的HTTP方法(如[HttpGet])

还有一个选项是将默认的基于Web API谓词的路由与传统方法混合使用,下面对此进行了详细描述:


    • 以防其他人面临此问题。我是这样解决的。使用控制器上的[Route]属性路由到特定url

      [路由(“api/getClient”)]
      公共ClientViewModel GetClient(int id)
      [路由(“api/GetAllClient”)]
      公共IEnumerable GetClients()
      
      Quick question,我是否可以这样路由,并且仍然只调用我的方法“Post”,并且如果我包含ActionNameAttribute,它们是否只能自动接受HttpPost?@Alxandr您仍然必须使用AcceptVerbsAttribute(或HttpPostAttribute、HttpGetAttribute等)。我在路由方面遇到一些问题-我可以定义多个“GET”方法,但是如果我点击/api/{controller},服务器会给出一个HTTP500“找到多个操作”,而不是404。你知道怎么阻止吗?我想要的是将/api/{controller}/{id}路由到“Get、Post、Put、Delete等”,然后将/api/{controller}/{id}/{action}路由到特定的操作,例如/api/Customers/5/Products。不起作用-所有内容都会出现“找到多个操作”错误。@uriDium看起来您正在寻找混合传统和基于动词的路由,您可以在此处阅读更多信息:@Si8请提供更多详细信息或通过电子邮件与我联系-我将尝试帮助。对于那些对这种格式更感兴趣的人,路由属性链接: