When I try to access my WCF service at the following address it works fine:
http://localhost/webservices/main.svc
Great. But I want to be able to call this service in following way:
http://localhost/api/main
So I am attempting to put in the proper ServiceRoute within the RouteConfig definitions in my MVC. I am able to make the routing work when the routePrefix variable is singular. But I cannot get it to work when routePrefix it is a path such as, as in my case, I want to map a route to ~/api/main.
I've scoured around The Google and this particular question is not common. I am pretty much seeking to accomplish the similar to what is stated in the accepted answer of the following previously posted question on this topic:
Expose WCF services that belong to an Area in MVC app at a routed path
I also attempted to implement a route constraint as suggested here: http://geekswithblogs.net/michelotti/archive/2010/09/22/wcf-rest-services-inside-mvc-projects.aspx
But still get the following error:
No HTTP resource was found that matches the request URI 'http://localhost/api/main'.
No type was found that matches the controller named 'main'.
Here is what I've attempted so far:
Imports System.Web.Mvc
Imports System.Web.Routing
Imports System.ServiceModel.Activation
Imports MyServiceLibrary
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
[..other routing stuff...]
routes.Add(New ServiceRoute("api/main", New ServiceHostFactory(), _
GetType(MyServiceLibrary.main)))
routes.MapRoute("Default", "{controller}/{action}/{id}", New With { _
Key .controller = "Default1", _
Key .action = "Index", _
Key .id = UrlParameter.Optional}, _
New With {Key .controller = "^(?!main).*"})
[..other routing stuff...]
End Sub
End Class
Can't seem to figure out the fix on this.
EDIT 2:
When I first posted this question I had a problem with what turned out to be malformed VB syntax.
The original INCORRECT syntax:
routes.Add(new ServiceRoute("api/main", new ServiceHostFactory(), _
typeof(MyServiceLibrary.main)))
The CORRECT vb syntax should be:
routes.Add(New ServiceRoute("api/main", New ServiceHostFactory(), _
GetType(MyServiceLibrary.main)))
Notice that in VB you should use GetType instead of typeof. Fixing this was a good step forward, but did not solve the underlying problem.
EDIT 3:
Well, turns out if I modify the ServiceRoute as follows, then IT WORKS:
routes.Add(New ServiceRoute("main", New ServiceHostFactory(), _
GetType(MyServiceLibrary.main)))
So, the question is how to create the route i desire "api/main"...
EDIT 4:
I thought I might have found the solution here: Can a WCF ServiceRoute Route Prefix contain a path value?
The solution there notably implements WebServiceHostFactory instead of ServiceHostFactory. I tried it, but it didn't work for me.