Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose WCF services that belong to an Area in MVC app at a routed path

I've got a WCF service (lets say TestService.svc sitting inside the services directory of an Area in an MVC app. This area is combined into the main app. The area is called content.

The routes have been setup and the area works fine. To access the Index action on the Home controller I can do either:

http://my-host/areas/content/index/home

or

http://my-host/content/index/home

The SVC file however can only be accessed via:

http://my-host/areas/content/services/TestService.svc

The URL must include the areas directory, I can't access it directly via http://my-host/content/services/TestService.svc. If I try I am given an error 404.

Is there a way to setup the application so that it routes the SVC request through the same route table as the controllers? I don't want to have to use areas for the services.

like image 526
Michael Shimmins Avatar asked Oct 28 '10 21:10

Michael Shimmins


People also ask

What is route in MVC What is default route in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.

Where we define routes in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).


1 Answers

If you have the liberty to use .Net 4.0 you might want to consider making your WCF service available via a ServiceRoute rather than via a .svc file.

This will enable you to avoid having the TestService.svc file with a TestService.svc.cs code-behind. In your Global.asax.cs you will have the following:

public static void RegisterRoutes(RouteCollection routes, IUnityContainer container)
{
    ... other MVC route mapping ....
    routes.Add(new ServiceRoute("TestService", new ServiceHostFactory(), typeof(LoaEvents)));
}

Your service should then be accessible via http://my-host/TestService.

You might be able to change the "TestService" argument to "/content/services/TestService" or something that works better for your needs.

like image 137
James Webster Avatar answered Sep 23 '22 22:09

James Webster