我在IIS 7.5上使用WCF 4,并希望从我所有RESTful服务的URL中删除默认的.svc扩展名.我已经看到了使用Url Rewrite Module和IHttpModule记录的方法,但我不想采用这些方法.
我模糊地熟悉ASP.NET MVC中引入的Routes的概念,据我所知,它们现在已经从Net4中作为System.Web.Routing从MVC中抽象出来.但是在查看文档时,我需要将一个Global.asax文件添加到我的项目中,我并不是真的热衷于此.有没有其他方法来处理这个?
我也看过基于配置的激活功能,但这似乎只是消除了.svc文件,但仍然要求我在我的服务网址中使用.svc.
任何人都可以在这里总结我的选择,因为我的网址中不需要.svc吗?
当然,没问题:首先,阅读A Developer的Windows Communication Foundation简介 4中有关新WCF 4功能的所有内容.
您正在寻找的是无文件服务激活.这是你的新设置<system.serviceModel>,看起来像这样:
<serviceHostingEnvironment>
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="/YourService"
service="SomeNamespace.YourService"/>
</serviceActivations>
</serviceHostingEnvironment>
Run Code Online (Sandbox Code Playgroud)
基本上,您在*.svc文件(路径,服务中)中拥有的所有信息现在都在此配置部分中.
您应该可以在此处拨打此服务
http://yourserver/virtualdirectory/YourService
Run Code Online (Sandbox Code Playgroud)
现在 - 没有更多*.svc,没有凌乱的URL重写等 - 它只是简单的作品!
更新:它似乎没有那么好用,除非你进入并为你的相对路径添加一个*.svc扩展名 - 这样做会破坏整个目的!
如果要使用ASP.NET路由进行注册,请查看有关该主题的MSDN文档.您必须在应用程序启动时使用以下内容:Web应用程序global.asax.cs:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.Add(new ServiceRoute("YourService",
new WebServiceHostFactory(), typeof(SomeNamespace.YourService)));
}
Run Code Online (Sandbox Code Playgroud)
希望通过它,您将能够在没有任何*.svc文件扩展名的情况下启动并运行您的服务!
小智 5
只是为了总结答案并在那里再添加一点.您需要与上面相同的ASP.NET路由注册:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.Add(new ServiceRoute("YourService",
new WebServiceHostFactory(), typeof(SomeNamespace.YourService)));
}
Run Code Online (Sandbox Code Playgroud)
为了实现这一点,您需要为web.config添加更多内容.应将服务托管配置为与ASP.NET兼容.这可以通过向serviceHostingEnvironment元素添加aspNetCompatibiliyEnabled ="true"来完成:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
Run Code Online (Sandbox Code Playgroud)
希望这能澄清并提供一个更容易找到的解决方案.