本文介绍了在路由路径上公开属于 MVC 应用程序中的区域的 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务(假设 TestService.svc 位于 MVC 应用程序区域的 services 目录中.这个区域被组合到主app.这个区域叫做content.

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.

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

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

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

然而,SVC 文件只能通过以下方式访问:

The SVC file however can only be accessed via:

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

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

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.

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

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.

推荐答案

如果您可以自由使用 .Net 4.0,您可能需要考虑通过 ServiceRoute 而不是通过 .svc 文件.

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.

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

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)));
}

您的服务应该可以通过 http://my-host/TestService 访问.

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

您可以将 "TestService" 参数更改为 "/content/services/TestService" 或更适合您需要的内容.

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

这篇关于在路由路径上公开属于 MVC 应用程序中的区域的 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 10:44