I'm trying to build a Web API project using Visual Basic on Webforms.
I've noticed that in the global.asax file, RouteTable.Routes.MapHttpRoute is an option which is only available in the C# version of global.asax. When creating a VB version of the file, only .MapPageRoute
is available.
Is HTTP routing only available in a C# project, or is there another way to call this class?
MapHttpRoute is an extension method on RouteCollection, defined in the System.Web.Http namespace. You need an Imports statement (in Global.asax or in the config file) to bring this namespace in scope. My guess is that your C# project has it while your VB
version doesn't. Add the following to your Global.asax and MapHttpRoute will light up:
That's odd. Does your project have a reference to the Sysrtem.Web.Http assembly? Can you compare the differences in references between the VB and C# projects?
I just tested this in VS 2012 and it works fine for VB....
I'm running Visual Web Developer 2010. It is weird that MapHttpRoute only shows up in a C# version of global.asax. I note that it's written as "(extension)" and I've read that MapHttpRoute is part of HttpRouteCollectionExtensions. Maybe this extension
wasn't written to be available for use in VB?
Extension methods are a framework feature, and as such are usable by C# as well as VB, as long as you reference the assembly that defines them and import their namespace.
Can you describe how you're creating your project? I just did this:
1. Started Visual Studio 2010
2. Choose new MVC 4 application
3. Chose Web API as the template and VB as the language
4. Opened up Global.asax which contains referencs to MapHttpRoute that all compile fine.
One thing that just struck me: is your IntelliSense displaying the Common or All list? MapHttpRoute only apears in the latter. You can switch between the two at the bottom of the IntelliSense list.
invervegas
Member
31 Points
41 Posts
RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 01, 2012 01:47 AM|LINK
I'm trying to build a Web API project using Visual Basic on Webforms.
I've noticed that in the global.asax file, RouteTable.Routes.MapHttpRoute is an option which is only available in the C# version of global.asax. When creating a VB version of the file, only .MapPageRoute is available.
Is HTTP routing only available in a C# project, or is there another way to call this class?
Imar_Spaanja...
Contributor
2784 Points
481 Posts
ASPInsiders
MVP
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 01, 2012 08:09 AM|LINK
Hi there,
MapHttpRoute is an extension method on RouteCollection, defined in the System.Web.Http namespace. You need an Imports statement (in Global.asax or in the config file) to bring this namespace in scope. My guess is that your C# project has it while your VB version doesn't. Add the following to your Global.asax and MapHttpRoute will light up:
Imports System.Web.Http
Hope this helps,
Imar
My Blog - My Company
invervegas
Member
31 Points
41 Posts
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 01, 2012 09:24 AM|LINK
I've already imported System.Web.Routing and System.Web.Http. Still don't have MapHttpRoute as an option.
Imar_Spaanja...
Contributor
2784 Points
481 Posts
ASPInsiders
MVP
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 01, 2012 09:43 AM|LINK
That's odd. Does your project have a reference to the Sysrtem.Web.Http assembly? Can you compare the differences in references between the VB and C# projects?
I just tested this in VS 2012 and it works fine for VB....
Cheers,
Imar
My Blog - My Company
invervegas
Member
31 Points
41 Posts
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 02, 2012 07:39 AM|LINK
I'm running Visual Web Developer 2010. It is weird that MapHttpRoute only shows up in a C# version of global.asax. I note that it's written as "(extension)" and I've read that MapHttpRoute is part of HttpRouteCollectionExtensions. Maybe this extension wasn't written to be available for use in VB?
Imar_Spaanja...
Contributor
2784 Points
481 Posts
ASPInsiders
MVP
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 02, 2012 01:12 PM|LINK
Extension methods are a framework feature, and as such are usable by C# as well as VB, as long as you reference the assembly that defines them and import their namespace.
Can you describe how you're creating your project? I just did this:
1. Started Visual Studio 2010
2. Choose new MVC 4 application
3. Chose Web API as the template and VB as the language
4. Opened up Global.asax which contains referencs to MapHttpRoute that all compile fine.
One thing that just struck me: is your IntelliSense displaying the Common or All list? MapHttpRoute only apears in the latter. You can switch between the two at the bottom of the IntelliSense list.
Cheers,
Imar
My Blog - My Company
niklasbengts...
Member
2 Points
1 Post
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Jul 04, 2012 05:59 AM|LINK
Make sure you are referencing System.Web.Http.Webhost in the project, and that you are importing System.Web.Http and System.Web.Routing.
Without referencing System.Web.Http.Webhost the project won't find the extensionmethod MapHttpRoute on the System.Web.Http assembly.
I'm not sure why this is but it solved the problem for me.
CHAMIN.G
Member
2 Points
1 Post
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Aug 07, 2012 04:35 AM|LINK
niklasbengtsson : I tried in your way but not solved.
invervegas : Did you sloved it?
Imar_Spaanjaars: Any ideas on this?
This is totally mess up to implement this in to VB.net. I have given reference and import libraries correctly.
At the moment I’m working on VS2010.
Edit : actually i'm working on a VB Asp.net Web Site
Durazi
Member
3 Points
2 Posts
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
Aug 15, 2012 03:51 AM|LINK
Hi Dear,
I've faced smae issue, and .... will .... it's kind of annoying thing ... but got you the solution...
Actully it seems intellisence crash only... :)
the method MapHttpRoute is availble in VB.NET assembly , but for some reason , Vs 2010 Intellisence wont detect it .... So just Give it a try
type it manually and VS will not raise you any errors or warnings.... and it will work ... or just copy it from here
RouteTable.Routes.MapHttpRoute("Your Api Name as string", "Your RouteTemplate as string" , RouteParameter option)Example Code:
RouteTable.Routes.MapHttpRoute("MyApi", "api/{controller}")and everything will be O.K.
I tried it my self... working !!!
kevinbky
Member
7 Points
2 Posts
Re: RouteTable.Routes.MapHttpRoute only available in C#, not VB
May 01, 2013 10:22 PM|LINK
another good reason to user c# :)