簡體   English   中英

具有干凈URL的WCF REST

[英]WCF REST with a clean URL

我正在考慮托管基於IIS 7的WCF Rest Service。訪問我的服務的URL將類似於

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

最近,我一直在尋找一些使用干凈URL的REST API實現,例如Yahoo API

social.yahooapis.com/v1/user/{guid}/contacts

考慮到我不想在URL中包含應用程序名稱和.svc,因此我想知道最好的WCF主機環境(例如Windows Service)或任何解決方案(例如URL重寫模塊)是什么,以便我可以擁有一個完全干凈的URL對於我的REST API

您可以在.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

您可以使用IIS 7中的URL重寫模塊來實現此目的。

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

是一個很好的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM