Asp.net WCF REST具有干净的URL
我正在考虑托管我在IIS7上构建的WCF Rest服务。访问我的服务的URL类似于 api.mycompany.com/applicationName/Service.svc/users/1347 最近,我一直在寻找一些RESTAPI实现,它有一个干净的URL,比如YahooAPI social.yahooapi.com/v1/user/{guid}/contactsAsp.net WCF REST具有干净的URL,asp.net,wcf,iis,rest,uri,Asp.net,Wcf,Iis,Rest,Uri,我正在考虑托管我在IIS7上构建的WCF Rest服务。访问我的服务的URL类似于 api.mycompany.com/applicationName/Service.svc/users/1347 最近,我一直在寻找一些RESTAPI实现,它有一个干净的URL,比如YahooAPI social.yahooapi.com/v1/user/{guid}/contacts 我想知道什么是最好的WCF主机环境(例如Windows服务)或任何解决方案(例如URL重写模块)考虑到我不想在我的URL中包含应
我想知道什么是最好的WCF主机环境(例如Windows服务)或任何解决方案(例如URL重写模块)考虑到我不想在我的URL中包含应用程序名和.svc,这样我就可以为我的REST API提供一个完全干净的URL,您可以使用IIS 7中的来实现这一点。您可以使用UriTemplate类,也可以查看WCF REST初学者工具包
这是一个很好的解释。您可以使用.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;
}
}
现在,您的服务的URL将是:
http://localhost/SampleApp/RestService/GetCollection
现在您有了干净和正确的REST URL
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