【问题标题】:How to remove the ".svc" extension in RESTful WCF service?如何删除 RESTful WCF 服务中的“.svc”扩展名?
【发布时间】:2010-09-26 04:35:49
【问题描述】:

据我所知,RESTful WCF 在其 URL 中仍然包含“.svc”。

例如,如果服务接口是这样的

[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。

如何在访问 URI 中删除这个“.svc”主机?

【问题讨论】:

标签: wcf rest


【解决方案1】:

我知道这篇文章现在有点老了,但如果你碰巧使用的是 .NET 4,你应该看看使用 URL Routing(在 MVC 中引入,但在核心 ASP.NET 中引入)。

在您的应用启动 (global.asax) 中,只需使用以下路由配置行来设置默认路由:

RouteTable.Routes.Add(new ServiceRoute("mysvc", new WebServiceHostFactory(), typeof(MyServiceClass)));

那么您的网址将如下所示:

http://servername/mysvc/value/2

HTH

【讨论】:

  • 更清洁的解决方案。谢谢!
  • 最佳解决方案(如果使用4.0)
  • 这在 asp.net mvc 4 中失败,因为 mvc 生成的所有其他链接现在都将指向该服务。
  • 如果您的项目不存在,请不要忘记在您的项目中添加对System.ServiceModel.Activation 的引用。 (VS2012 中的默认 WCF 项目似乎没有包含这个)
  • 别忘了将它添加到 Global.asax.cs:using System.Web.Routing; using System.ServiceModel.Activation;
【解决方案2】:

在 IIS 7 中,您可以使用 Url Rewrite Module,如本博客 post 中所述。

在 IIS 6 中,您可以编写一个 http module 来重写 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);
            }
        };
    }
}

还有一个很好的example 如何在不使用第三方 ISAPI 模块或通配符映射的情况下在 IIS 6 中实现无扩展 url。

【讨论】:

  • int i = path.IndexOf('/', 2);必须是 int i = path.LastIndexOf('/', 2);
【解决方案3】:

以下是使用 IIS 7 重写模块或使用自定义模块的更详细信息: http://www.west-wind.com/Weblog/posts/570695.aspx

【讨论】:

    【解决方案4】:

    还有一种方法可以完全消除物理 .svc 文件。这可以通过VirtualPathProvider 完成。

    见:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/350f2cb6-febd-4978-ae65-f79735d412db

    【讨论】:

      【解决方案5】:

      在 IIS 7 上很容易 - 使用 URL Rewrite Module

      在 IIS 6 上,我发现使用 ISAPI Rewrite module 最简单,它允许您定义一组将请求 URL 映射到 .svc 文件的正则表达式...

      【讨论】:

        【解决方案6】:

        在 IIS6 或 7 中,您可以使用免费的重写过滤器IIRF。这是我使用的规则:

        # Iirf.ini
        #
        
        RewriteEngine ON
        RewriteLog  c:\inetpub\iirfLogs\iirf-v2.0.services
        RewriteLogLevel 3
        StatusInquiry  ON  RemoteOk
        CondSubstringBackrefFlag *
        MaxMatchCount 10
        
        # remove the .svc tag from external URLs
        RewriteRule  ^/services/([^/]+)(?<!\.svc)/(.*)$    /services/$1.svc/$2  [L]
        

        【讨论】:

          【解决方案7】:

          将此添加到您的 global.asax

          private void Application_BeginRequest(object sender, EventArgs e)
          {
              Context.RewritePath(System.Text.RegularExpressions.Regex.Replace(
                         Request.Path, "/rest/(.*)/", "/$1.svc/"));
          }
          

          这会将 /rest/Service1/arg1/arg2 替换为 /Service1.svc/arg1/arg2

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-08-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-25
            • 2014-08-21
            • 1970-01-01
            相关资源
            最近更新 更多