在路由路径中公开属于MVC应用程序中的Area的WCF服务

Mic*_*ins 14 asp.net-mvc wcf

我有一个WCF服务(假设TestService.svc坐在servicesMVC应用程序中的Area目录中.这个区域被合并到主应用程序中.该区域被调用content.

路线已经设置,该区域工作正常.要访问控制器Index上的操作,Home我可以执行以下任一操作:

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

要么

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

但是,SVC文件只能通过以下方式访问:

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

URL必须包含areas目录,我无法直接访问它http://my-host/content/services/TestService.svc.如果我尝试,我会收到错误404.

有没有办法设置应用程序,以便它通过与控制器相同的路由表路由SVC请求?我不想areas用于服务.

Jam*_*ter 24

如果您可以自由使用.Net 4.0,则可能需要考虑通过ServiceRoute而不是.svc文件来提供WCF服务.

这将使您能够避免使用TestService.svc.cs代码隐藏的TestService.svc文件.在Global.asax.cs中,您将拥有以下内容:

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

然后,您可以通过访问您的服务http://my-host/TestService.

您可以将"TestService"参数更改为"/content/services/TestService"更适合您需求的参数.