2013-01-11 44 views
1

我創建了一個啓用了路由的簡單休息服務。當我在本地運行它時,路由正常工作,即使用asp.net開發服務器。但是,當我在IIS(IIS 7.5)部署應用程序,然後它嘗試訪問該服務中的方法我得到錯誤HTTP 404.0未找到。這裏是我的代碼:ASP.NET在休息服務中路由不在IIS中工作

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class HelloWorldService 
{ 

    [WebGet(UriTemplate = "Date")] 
    public DateTime Date() 
    { 
     return System.DateTime.Now; 
    } 

} 

的Global.asax:

protected void Application_Start(object sender, EventArgs e) 
    { 
     RegisterRoutes(); 
    } 
    private void RegisterRoutes() 
    {   
     RouteTable.Routes.Add(new ServiceRoute("ServiceData", new WebServiceHostFactory(), typeof(HelloWorldService))); 
    } 

Web.config文件:

<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web> 

<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> 
      <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
</system.serviceModel> 

</configuration> 

我還啓用了HTTP重定向功能下

Windows功能 - > Internet信息服務 - > Word Wide Web服務 - >通用HTTP功能

我也嘗試添加處理程序一樣

<handlers> 
    <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, 
     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
</handlers> 

我也有試過被建議在網絡上但沒有任何工程的所有其他解決方案。預先感謝您的幫助。

回答

0

你的RegisterRoutes()有些問題。它應該是:

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

enter image description here

+0

如果某事是錯的,我想我不應該,即使在本地主機上運行。反正我試過,但它仍然無法正常工作。 –

+0

我可以知道你使用的是什麼模板嗎?我建議使用WCF REST服務模板40(CS)。除了RegisterRoute()之外,你不需要配置任何東西。也許你的一些配置導致了這個錯誤。請參閱我在答案中添加的圖片。 –

+0

我總是使用'​​ASP.NET空Web通信'。我甚至沒有.svc文件在我的解決方案,因爲我已經閱讀的地方沒有需要它和休息服務可以像正常的asp.net網站託管。 –

相關問題