vb.net 如何访问 RouteTable.Routes.MapHttpRoute?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18583784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access RouteTable.Routes.MapHttpRoute?
提问by Michael Richardson
I have a Web Forms app that I created a few months ago and I added a Web API controller. I tried to use the 'automatic' routing that I saw in a presentation recently, but all I got was a 404. Then I tried to add routing for the Web API controller in my Global.asax using MapHttpRoute, as I've seen in several tutorials. However, even after adding Imports System.Web.Httpto the file, the project will not recognize RouteTable.Routes.MapHttpRoute()I have tried adding other namespaces and ensuring that I have all the necessary Nuget packages, but I still am unable to set up routing for the Web API controller. Does anyone have a recommendation of where to start?
我有一个几个月前创建的 Web 窗体应用程序,并添加了一个 Web API 控制器。我尝试使用最近在演示文稿中看到的“自动”路由,但我得到的只是 404。然后我尝试使用 MapHttpRoute 在 Global.asax 中为 Web API 控制器添加路由,正如我在几个教程。但是,即使添加Imports System.Web.Http到文件后,项目也不会识别出RouteTable.Routes.MapHttpRoute()我已尝试添加其他命名空间并确保我拥有所有必需的 Nuget 包,但我仍然无法为 Web API 控制器设置路由。有没有人有关于从哪里开始的建议?
采纳答案by woz
I found this thread, which indicates that IntelliSense apparently has a problem with this, but if you type something like the following, it will build and run:
我发现了这个线程,这表明 IntelliSense 显然有这个问题,但是如果您键入如下内容,它将构建并运行:
RouteTable.Routes.MapHttpRoute("MyApi", "api/{controller}")
回答by spryce
If anyone has this same issue in C# read on.
如果有人在 C# 中遇到同样的问题,请继续阅读。
I am also using a web forms app and set the routing through the Global.asaxfile. For me Intellisense was 100% correct and it would not build regardless. To get this to work I had to add the following using directives.
我也在使用 Web 表单应用程序并通过Global.asax文件设置路由。对我来说,Intellisense 是 100% 正确的,无论如何它都不会构建。为了让它工作,我必须添加以下 using 指令。
using System.Web.Http;
and
和
using System.Web.Routing;
Becareful notto use using System.Web.Http.Routingby accident. This wont work.
Becareful不使用using System.Web.Http.Routing偶然。这行不通。
回答by Martin Kunc
You should add the reference to System.Web.Http.WebHostassembly and make sure you have
您应该添加对System.Web.Http.WebHost程序集的引用,并确保您有
using System.Web.Http;
Why ? MapHttpRouteis defined in two assemblies, in System.Web.Http:
为什么 ?MapHttpRoute在两个程序集中定义,在System.Web.Http:
public static System.Web.Http.Routing.IHttpRoute MapHttpRoute(
this System.Web.Http.HttpRouteCollection routes, string name, string routeTemplate)
Member of System.Web.Http.HttpRouteCollectionExtensions
成员 System.Web.Http.HttpRouteCollectionExtensions
and in System.Web.Http.WebHost
并在 System.Web.Http.WebHost
public static Route MapHttpRoute(
this RouteCollection routes, string name, string routeTemplate, object defaults);
First one is an Extension on HttpRouteCollection
第一个是HttpRouteCollection上的扩展
Second one is an Extension on RouteCollection
第二个是对RouteCollection的扩展
So when you have a webforms app, your Routes are defined in a RouteCollectionso you need the WebHostversion.
所以当你有一个 webforms 应用程序时,你的路由是在 a 中定义的,RouteCollection所以你需要WebHost版本。
Its because architecture allowing WebApi to be hosted also out of IIS. see Hosting WebApi
这是因为架构允许 WebApi 也托管在 IIS 之外。请参阅托管 WebApi
回答by Will L
I've just created two new webforms apps (one using .NET 4, the other 4.5), did nothing more than add web api via nuget and it worked.
我刚刚创建了两个新的 webforms 应用程序(一个使用 .NET 4,另一个使用 4.5),只是通过 nuget 添加了 web api 并且它起作用了。
What version of ASP.NET is your app running in? If you're running ASP.NET WebForms 2.0/3.5 then it's not supported.
您的应用程序在哪个版本的 ASP.NET 中运行?如果您运行的是 ASP.NET WebForms 2.0/3.5,则它不受支持。
Here's a tutorial which demonstrates how to add Web API to your Web Forms app - http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms
这是一个演示如何将 Web API 添加到您的 Web 窗体应用程序的教程 - http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-形式
回答by elhag Hassan
I have the same case, I solved by creating the api controller, when you add the api controller VS will load all required refrences.
我有同样的情况,我通过创建 api 控制器解决了,当您添加 api 控制器时,VS 将加载所有必需的引用。

