简体   繁体   English

ASP.NET Web表单MapHttpRoute中的WEB API

[英]WEB API in asp.net web forms MapHttpRoute

I've been racking on my brain on this for a little bit now. 我已经为此花了一点时间。 I have built a web api, and I am now moving it to a web forms application. 我已经构建了一个Web API,现在将其移至Web窗体应用程序。 Every tutorial I have read says do it the way I am attempting to do. 我读过的每个教程都按照我尝试的方式做。 I have also checked the web for an answer to this, but they all suggest things I have already tried. 我也在网上检查了答案,但是他们都建议我已经尝试过了。 The error I am getting is: 我得到的错误是:

System.Web.Routing.RouteCollection does not contain a definition for MapHttpRoute

Here are my namespaces and my Application_Start method: 这是我的名称空间和Application_Start方法:

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.Http" %>
<%@ Import Namespace="System.Web.Routing" %>

<script language="C#" runat="server">

void Application_Start(Object sender, EventArgs e) {


    RouteTable.Routes.MapHttpRoute(
           name: "apiCall",
           routeTemplate: "api/{controller}/{key}/{args}",
           defaults: new { args = System.Web.Http.RouteParameter.Optional }
       );

}

Any help as to what I am doing wrong would be greatly appreciated. 任何关于我做错事的帮助将不胜感激。

Are you sure you have the correct using statement? 您确定使用正确的using语句吗? I think this is the one you want: 我认为这是您想要的:

http://msdn.microsoft.com/en-us/library/hh835153(v=vs.118).aspx http://msdn.microsoft.com/zh-CN/library/hh835153(v=vs.118).aspx

Thanks for the hepl, but I ended up adding my web api to my project through Visual Studio, and it fixed itself. 感谢您的帮助,但是最后我通过Visual Studio将Web api添加到我的项目中,并对其进行了修复。 I had just been trying to add in windows explorer. 我刚刚尝试添加Windows资源管理器。

I had this problem and I fixed it by adding this assembly reference : System.Web.Http.WebHost 我遇到了这个问题,并通过添加以下程序集引用对其进行了修复:System.Web.Http.WebHost

组装参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM