Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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# 在API控制器中调用自定义方法_C#_Asp.net_Asp.net Web Api_Asp.net Web Api2_Asp.net Web Api Routing - Fatal编程技术网

C# 在API控制器中调用自定义方法

C# 在API控制器中调用自定义方法,c#,asp.net,asp.net-web-api,asp.net-web-api2,asp.net-web-api-routing,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,Asp.net Web Api Routing,我在web api控制器中添加了一个方法: public String GetpeopleInFamily(string familyId) 我从这个问题中列举了一些例子: 我根据上述内容更新了RouteConfig.cs文件: 公共静态无效注册表项(路由收集路由) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”) 但调用该方法时,我仍然得到404: 请求GET/api/people/GetpeopleInFamily/101 HTTP/1

我在web api控制器中添加了一个方法:

public String GetpeopleInFamily(string familyId)
我从这个问题中列举了一些例子:

我根据上述内容更新了RouteConfig.cs文件: 公共静态无效注册表项(路由收集路由) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”)

但调用该方法时,我仍然得到404:

请求GET/api/people/GetpeopleInFamily/101 HTTP/1.1

{“Message”:“未找到与请求URI匹配的HTTP资源 ' “.”,“MessageDetail”:“在控制器“人员”上未找到任何操作” 与请求匹配的


我需要做些什么才能使其工作?此外,添加返回更复杂数据集的自定义方法的最佳做法是什么?

路由中没有映射
familyId
。因此,您可以创建一个新映射,将familyId添加到id映射中,或者将
GetPeopleInFamily
的参数重写为
id
>

重写:

public string GetPeopleInFamily(string id)
创建新路由:创建新路由时,请确保使用
id
删除映射,否则它将无法工作

routes.MapHttpRoute("DefaultApiWithActionAndFamilyId", "Api/{controller}/{action}/{familyId}", new { familyId = RouteParameter.Optional }, new { familyId = @"\d+" });

您需要配置web API路由以接受familyId参数

config.Routes.MapHttpRoute(name: "newont",
                routeTemplate: "api/{controller}/{familyId}/{id}",
                defaults: new { controller = "Controllername", familyId= "familyId", new { id = RouteParameter.Optional }}
            );
config.Routes.MapHttpRoute(name: "newont",
                routeTemplate: "api/{controller}/{familyId}/{id}",
                defaults: new { controller = "Controllername", familyId= "familyId", new { id = RouteParameter.Optional }}
            );