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

后端 未结 7 903
借酒劲吻你
借酒劲吻你 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.

提交回复
热议问题