我有一个 WCF 服务(可以说 TestService.svc
位于 MVC 应用程序中区域的 services
目录中。该区域组合到主应用程序中。该区域称为 content
。
路线已经设置好,该区域运行良好。访问 Index
对 Home
的操作 Controller 我可以执行以下任一操作: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。
有没有办法设置应用程序,使其通过与 Controller 相同的路由表路由 SVC 请求?我不想必须使用 areas
对于服务。
最佳答案
如果您可以自由使用 .Net 4.0,您可能需要考虑通过 ServiceRoute 提供您的 WCF 服务。而不是通过 .svc 文件。
这将使您能够避免 TestService.svc 文件带有 TestService.svc.cs 代码隐藏。在您的 Global.asax.cs 中,您将拥有以下内容:
public static void RegisterRoutes(RouteCollection routes, IUnityContainer container)
{
... other MVC route mapping ....
routes.Add(new ServiceRoute("TestService", new ServiceHostFactory(), typeof(LoaEvents)));
}
然后应该可以通过
http://my-host/TestService
访问您的服务.您也许可以更改
"TestService"
论据 "/content/services/TestService"
或者更适合您的需求的东西。
关于asp.net-mvc - 在路由路径上公开属于 MVC 应用程序中的区域的 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047485/