How to remove the “.svc” extension in RESTful WCF service?

后端 未结 7 843
借酒劲吻你
借酒劲吻你 2020-11-29 17:41

In my knowledge, the RESTful WCF still has \".svc\" in its URL.

For example, if the service interface is like

[OperationContract]
[WebGet(UriTemplate         


        
相关标签:
7条回答
  • 2020-11-29 17:55

    In IIS 7 you can use the Url Rewrite Module as explained in this blog post.

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

    And there's a nice example how to achieve extensionless urls in IIS 6 without using third party ISAPI modules or wildcard mapping.

    0 讨论(0)
  • 2020-11-29 18:02

    There is also a way to eliminate the physical .svc files altogether. This can be done with a VirtualPathProvider.

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

    0 讨论(0)
  • 2020-11-29 18:02

    In IIS6 or 7, you can use IIRF, a free rewriting filter. Here's the rule I used:

    # 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]
    
    0 讨论(0)
  • 2020-11-29 18:03

    Add this to your global.asax

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

    This will replace /rest/Service1/arg1/arg2 by /Service1.svc/arg1/arg2

    0 讨论(0)
  • 2020-11-29 18:14

    Here's more detailed info using the IIS 7 Rewrite Module, or using a custom module: http://www.west-wind.com/Weblog/posts/570695.aspx

    0 讨论(0)
  • 2020-11-29 18:15

    I know this post is a bit old now, but if you happen to be using .NET 4, you should look at using URL Routing (introduced in MVC, but brought into core ASP.NET).

    In your app start (global.asax), just have the following route configuration line to setup the default route:

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

    then your URLs would look like this:

    http://servername/mysvc/value/2
    

    HTH

    0 讨论(0)
提交回复
热议问题