【发布时间】:2016-11-22 18:01:48
【问题描述】:
我正在尝试向我们现有的 Web API 添加一个控制器。控制器就像
public class TDataController : ApiController
{
[HttpGet]
public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT, DateTime DOB_LT, string STATUS_TYPE)
{
// Code for the controller
}
}
这是我试图在同一个应用程序中添加的控制器
public class TDataSubDateController : ApiController
{
[HttpGet]
public HttpResponseMessage Getdetails(string ROOM, string STATUS_TYPE, DateTime? SUBDATE_GT = null, DateTime? SUBDATE_LT = null)
{
//Code for the controller
}
}
当我试图调用第二个控制器时
http://localhost:33823/api/TDataSubDate?ROOM=xxx&STATUS_TYPE=xxx&SUBDATE_GT=xxxx&SUBDATE_LT=xxxx
但它会抛出HTTP 404 Page Not Founderror。我是否必须在 WebConfig.cs 中创建不同的路由。 RouteConfig.cs 目前看起来像
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
【问题讨论】:
-
该路由配置是用于 MVC,而不是用于 Web API。
-
如何在同一个 API 中处理两个控制器。为了测试,我使用了相同的代码并创建了单独的 Web API,它运行良好并返回结果
-
您需要找到您的 Web API 路由的配置位置。如果您使用默认模板,可能是
WebApiConfig.cs。路由全部解释here。 -
我已经用 WebConfig.cs 编辑了我的问题。我在这里创建了一条新路线吗?
标签: c# asp.net asp.net-mvc asp.net-web-api