Greatings,
我的asp.net mvc4研究项目有问题。 我试图用多个get动作创建apicontroller,其中一些带参数,其他 - 没有。
我的api
public class ContentController : ApiController
{
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories1()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories2()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories3(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories4(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
}
我的apiConfig:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
以及
的结果/ api / content / categories1的结果是[“玩具”,“游戏”,“动物”,“Woob-Woob”]
for / api / content / categories3和/ api / content / categories3 / 1 result - “找不到与请求URI匹配的HTTP资源'http:// localhost:51200 / api / content / categories3'。”< / p>
我不知道应如何处理
答案 0 :(得分:1)
我认为它失败了,因为api操作中参数的名称与路径中参数的名称不匹配。
更改您的第一条路线:
config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
以下内容:
config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{i}",
defaults: new { i = RouteParameter.Optional }
);
看看是否有效。
通常,更具体的路线应位于Register方法的顶部。最常见的路线位于底部,是全能的。
考虑如下控制器:
public class UserSettingsController : ApiController
{
[HttpGet]
public HttpResponseMessage GetAll();
[HttpGet]
public HttpResponseMessage Get(string key);
[HttpGet]
public HttpResponseMessage Set(string key, string value);
[HttpGet]
public HttpResponseMessage Restore(string key);
}
要设置路线,需要以下路线。请注意,如果api调用上没有键,则会对它们进行排序,以便触发GetAll方法。如果前两个路由被切换,那么GetAll版本将永远不会被命中,因为对/ api / settings的调用将转到Get方法,并且该键将被设置为null。
config.Routes.MapHttpRoute(
name: "user/settings",
routeTemplate: "user/settings",
defaults: new { controller = "UserSettings", action = "GetAll" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
config.Routes.MapHttpRoute(
name: "user/settings/{key} (get)",
routeTemplate: "user/settings/{key}",
defaults: new { controller = "UserSettings", action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
config.Routes.MapHttpRoute(
name: "user/settings/{key} (set)",
routeTemplate: "user/settings/{key}",
defaults: new { controller = "UserSettings", action = "Set" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
);
config.Routes.MapHttpRoute(
name: "user/settings/{key}/restore",
routeTemplate: "user/settings/{key}/restore",
defaults: new { controller = "UserSettings", action = "Restore" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);