问题描述
我在 IIS 7.5 上使用 WCF 4,并希望从我所有 RESTful 服务的 URL 中删除默认的 .svc 扩展名.我已经看到使用 Url Rewrite Module 和 IHttpModule 记录的方法,但我不想采用这些方法.
I'm using WCF 4 on IIS 7.5 and want to eliminate the default .svc extension from the URL of all my RESTful services. I've seen the approaches documented using the Url Rewrite Module and an IHttpModule but I don't want to take those approaches.
我对 ASP.NET MVC 中引入的路由概念有些熟悉,据我所知,它们现在从 Net 4 中的 MVC 中抽象出来作为 System.Web.Routing.但是在查看文档时,我似乎需要在我的项目中添加一个 Global.asax 文件,但我并不是很喜欢这个文件.有没有其他办法可以解决这个问题?
I am vaguely familiar with the concept of Routes introduced in ASP.NET MVC and as I understand they are now abstracted out of MVC in Net 4 as System.Web.Routing. But in looking at the docs it appears I need to add a Global.asax file to my project which I'm not really keen on. Is there any other way to handle this?
我还看到了基于配置的激活功能,但这似乎只是消除了 .svc 文件,但仍然要求我在服务的 url 中使用 .svc.
I've also seen the Configuration-Based activation feature, but that only seems to eliminate the .svc file, but still requires I use .svc in the url of my service.
有人可以在这里总结一下我在网址中不需要 .svc 的选择吗?
Can anyone summarize my options here for not needing .svc in my urls?
推荐答案
当然,没问题:首先,阅读 开发人员对 Windows Communication Foundation 4 的介绍.
Sure, no problem: first off, read all about the new WCF 4 features in A Developer's Introduction to Windows Communication Foundation 4.
您要查找的内容称为无文件服务激活.这是 中的一个新设置,如下所示:
What you're looking for is called file-less service activation. It's a new setting in your <system.serviceModel>
that looks something like this:
<serviceHostingEnvironment>
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="/YourService"
service="SomeNamespace.YourService"/>
</serviceActivations>
</serviceHostingEnvironment>
基本上,您在 *.svc 文件中拥有的所有信息(路径、要调用的服务)现在都在此配置部分中.
Basically, all the information you'd have in the *.svc file (path, service to call) is in this config section now.
您应该可以在
http://yourserver/virtualdirectory/YourService
现在 - 没有更多的 *.svc,没有凌乱的 URL 重写等 - 它只是简单的工作!
now - no more *.svc, no messy URL rewriting etc. - it just plain works!
更新:它似乎没有那么好用,除非你进入并在你的相对路径中添加一个 *.svc 扩展名 - 有点违背了整个目的!
Update: it doesn't seem to work that well, unless you go in and add a *.svc extension to your relative path - kind defeats the whole purpose!
如果您想使用 ASP.NET 路由进行注册,请查看 MSDN有关该主题的文档.你必须在你的应用程序启动时有这样的东西,在一个 global.asax.cs
的网络应用程序中:
If you want to register using an ASP.NET route, check out the MSDN docs on that topic. You'd have to have something like this in your application startup, in a web app that would be 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)));
}
希望如此,您将能够在没有任何 *.svc 文件扩展名的情况下启动并运行您的服务!
Hopefully, with that, you'll be able to get your service up and running without any *.svc file extensions!
这篇关于使用路由消除 WCF 4 服务 URL 中的 .svc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!