问题描述
据我所知,RESTful WCF 的 URL 中仍然包含.svc".
In my knowledge, the RESTful WCF still has ".svc" in its URL.
例如,如果服务接口像
[OperationContract]
[WebGet(UriTemplate = "/Value/{value}")]
string GetDataStr(string value);
访问 URI 类似于 "http://machinename/Service.svc/Value/2".在我的理解中,REST 的部分优势在于它可以隐藏实现细节.像http://machinename/Service/value/2"这样的 RESTful URI 可以由任何 RESTful 实现框架,但是http://machinename/Service.svc/value/2" 公开了它的实现是 WCF.
The access URI is like "http://machinename/Service.svc/Value/2". In my understanding, part of REST advantage is that it can hide the implementation details. A RESTful URI like "http://machinename/Service/value/2" can be implemented by any RESTful framework, but a "http://machinename/Service.svc/value/2" exposes its implementation is WCF.
如何在访问 URI 中删除这个.svc"主机?
How can I remove this ".svc" host in the access URI?
推荐答案
在 IIS 7 中,您可以使用 Url 重写模块,如本博客中所述发布.
In IIS 7 you can use the Url Rewrite Module as explained in this blog post.
在 IIS 6 中,您可以编写一个 http 模块 这将重写网址:
In IIS 6 you could write an http module that will rewrite the url:
public class RestModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication app)
{
app.BeginRequest += delegate
{
HttpContext ctx = HttpContext.Current;
string path = ctx.Request.AppRelativeCurrentExecutionFilePath;
int i = path.IndexOf('/', 2);
if (i > 0)
{
string svc = path.Substring(0, i) + ".svc";
string rest = path.Substring(i, path.Length - i);
ctx.RewritePath(svc, rest, ctx.Request.QueryString.ToString(), false);
}
};
}
}
还有一个很好的示例如何实现无扩展名的网址在 IIS 6 中不使用第三方 ISAPI 模块或通配符映射.
And there's a nice example how to achieve extensionless urls in IIS 6 without using third party ISAPI modules or wildcard mapping.
这篇关于如何删除“.svc"文件RESTful WCF 服务中的扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!