带有干净URL的WCF REST

时间:2010-08-24 15:57:45

标签: asp.net wcf iis rest uri

我正在考虑托管我在IIS 7上构建的WCF Rest服务。访问我的服务的URL将类似于

api.mycompany.com/applicationName/Service.svc/users/1347

最近,我一直在寻找一些REST API实现,其中包含一个干净的URL,如Yahoo API

social.yahooapis.com/v1/user/ {GUID} /联系人

我想知道什么是最好的WCF主机环境(例如Windows服务)或任何解决方案(例如URL重写模块),因为我不想在我的URL中有应用程序名称和.svc以便我可以拥有完全清理我的REST API的URL

3 个答案:

答案 0 :(得分:1)

您可以在.NET 4中使用新的WebApi模板,该模板允许您在global.asax中指定路由。这完全摆脱了svc文件。您需要AspNetCompatilbityMode = true。请参阅以下示例:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class RestService
{
    // TODO: Implement the collection resource that will contain the SampleItem instances

    private static List<SampleItem> sampleCollection = new List<SampleItem>();

    [WebGet]
    public List<SampleItem> GetCollection()
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        if (sampleCollection.Count == 0)
        {
            sampleCollection = new List<SampleItem>();
            sampleCollection.Add(new SampleItem() { Id = 1, StringValue = "Hello 1" });
            sampleCollection.Add(new SampleItem() { Id = 2, StringValue = "Hello 2" });
            sampleCollection.Add(new SampleItem() { Id = 3, StringValue = "Hello 3" });
            sampleCollection.Add(new SampleItem() { Id = 4, StringValue = "Hello 4" });
            sampleCollection.Add(new SampleItem() { Id = 5, StringValue = "Hello 5" });
        }
        return sampleCollection;
    }
}

你的web.config将会有以下内容:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>
<system.serviceModel>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->        
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

最后你的Global.asax如下:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        // Edit the base address of Service1 by replacing the "Service1" string below
        RouteTable.Routes.Add(new ServiceRoute("RestService", new WebServiceHostFactory(), typeof(RestService)));
    }
}

现在您服务的网址为:

http://localhost/SampleApp/RestService/GetCollection

现在您拥有干净且正确的REST URL

答案 1 :(得分:0)

您可以使用IIS 7中的URL rewrite module来实现此目的。

答案 2 :(得分:0)

您可以使用UriTemplate Class查看WCF Rest Starter Kit。

Here是一个很好的解释。