简体   繁体   English

具有干净URL的WCF REST

[英]WCF REST with a clean URL

I'm considering to host WCF Rest Service that i've built on IIS 7. The URL to access my service will be something like 我正在考虑托管基于IIS 7的WCF Rest Service。访问我的服务的URL将类似于

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

Recently, i've been looking to some REST API implementation with a clean URL like Yahoo API 最近,我一直在寻找一些使用干净URL的REST API实现,例如Yahoo API

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

I'm wondering what will be the best WCF host environment (eg Windows Service) or any solution (eg URL rewrite module) considering that I dont want to have application name and .svc in my URL so that I can have a completely clean URL for my REST API 考虑到我不想在URL中包含应用程序名称和.svc,因此我想知道最好的WCF主机环境(例如Windows Service)或任何解决方案(例如URL重写模块)是什么,以便我可以拥有一个完全干净的URL对于我的REST API

You can use the new WebApi template in .NET 4 that provides you to specify the route in the global.asax. 您可以在.NET 4中使用新的WebApi模板,该模板为您提供在global.asax中指定路由的功能。 This gets rid of the svc file completely. 这完全摆脱了svc文件。 You need to have AspNetCompatilbityMode = true. 您需要具有AspNetCompatilbityMode = true。 See the sample below: 请参见下面的示例:

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

You web.config will have teh following: 您的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>

And finally your Global.asax as follows: 最后,您的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)));
    }
}

Now the URL for your service would be : 现在,您的服务的网址为:

http://localhost/SampleApp/RestService/GetCollection

Now you have clean and proper REST URLs 现在您有了干净正确的REST URL

您可以使用IIS 7中的URL重写模块来实现此目的。

You can use UriTemplate Class also look into the WCF Rest Starter Kit. 您可以使用UriTemplate类也可以查看WCF Rest Starter Kit。

Here is a good explanation. 是一个很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM