更新时间:2022-11-06 17:34:13
我需要执行的步骤是:
System.Web.Http.WebHost
。 App_Start \\ WebApiConfig.cs
(见下文code段)。 System.Web.Http
在的Global.asax.cs
。 WebApiConfig.Register(GlobalConfiguration.Configuration)
在 MvcApplication.Application_Start()
(在文件的Global.asax.cs
)的前的注册默认Web应用程序的路由,否则将采取precedence。 System.Web.Http.ApiController
导出。System.Web.Http.WebHost
.App_Start\WebApiConfig.cs
(see code snippet below).System.Web.Http
in Global.asax.cs
.WebApiConfig.Register(GlobalConfiguration.Configuration)
in MvcApplication.Application_Start()
(in file Global.asax.cs
), before registering the default Web Application route as that would otherwise take precedence.System.Web.Http.ApiController
.然后我可以从the教程(你的第一个的ASP.NET Web API)来定义我的API控制器。
I could then learn enough from the tutorial (Your First ASP.NET Web API) to define my API controller.
App_Start \\ WebApiConfig.cs:
App_Start\WebApiConfig.cs:
using System.Web.Http;
class WebApiConfig
{
public static void Register(HttpConfiguration configuration)
{
configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
}
}
的Global.asax.cs:
Global.asax.cs:
using System.Web.Http;
...
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
WebApiConfig.Register(GlobalConfiguration.Configuration);
RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
更新2015年10月16日:
有传言,该包的NuGet Microsoft.AspNet.WebApi必须安装上述工作。
Word has it, the NuGet package Microsoft.AspNet.WebApi must be installed for the above to work.