【发布时间】:2017-01-26 13:10:13
【问题描述】:
我一直在构建 WebAPI,尝试使用 ActionName 路由到正确的方法。它适用于我尝试调用的一种方法,但另一种方法出现 404 错误。
我的 WebAPI 配置文件:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我的 WebAPI 控制器方法的格式如下:
第一个是工作的:
[ActionName("postdb")]
public IEnumerable<string[]> postDB(string id)
{ ...
第二个没有:
[ActionName("getquery")]
public IEnumerable<string[]> getQuery(string tables)
{ ...
我在 Angular 中以相同的方式调用它们(Temp 是作为参数传递的字符串):
$http.post('api/Test/postdb/' + temp).then(function (response) { ...
和
$http.get('api/Test/getquery/' + temp).then(function (response) { ...
我尝试更改这两个动作的名称,第一个无论名称如何都有效,第二个无论名称如何都不起作用。我也尝试过重新排序,在 GET 和 POST 之间更改,以及更改参数。
有什么建议吗?
【问题讨论】:
标签: c# angularjs asp.net-mvc-4 asp.net-web-api