2015-09-02 46 views
0

我剛剛在我的項目中創建了一個基本的Web控制器。我打了調試,並嘗試瀏覽到/ api/duedate,我得到了404。我是控制器的新手,並一直在尋找我可以找到的每個教程。他們都沒有說我需要添加任何更多的東西才能使這個工作。簡單的Web API控制器返回404

Imports System.Net 
Imports System.Web.Http 

Public Class DueDateController 
    Inherits ApiController 

    ' GET api/duedate 
    Public Function GetValues() As IEnumerable(Of String) 
     Return New String() {"value1", "value2"} 
    End Function 

    ' GET api/duedate/5 
    Public Function GetValue(ByVal id As Integer) As String 
     Return "value" 
    End Function 

    ' POST api/duedate 
    Public Sub PostValue(<FromBody()> ByVal value As String) 

    End Sub 

    ' PUT api/duedate/5 
    Public Sub PutValue(ByVal id As Integer, <FromBody()> ByVal value As String) 

    End Sub 

    ' DELETE api/duedate/5 
    Public Sub DeleteValue(ByVal id As Integer) 

    End Sub 
End Class 

我的web.config文件看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    <add key="webPages:Version" value="2.0"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true" /> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers></system.webServer> 

</configuration> 

回答

1

我相信你需要添加路由。

Routing in VB

如果你讀這個線程最後一個註釋應該告訴你如何添加路由到您的應用程序。

+0

我的MapPageRoute用不可RouteTable.Routes.MapHttpRoute – dko

+0

你有相同的進口像他那樣?你可能錯過了一個參考? – kereberos

+0

我確認我擁有所有的參考和進口。我甚至嘗試添加額外的。沒有運氣 – dko

0

幫助其他人解決問題並簡化必須閱讀那篇長文章。您需要創建和修改Global.asax文件,包括此:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 
    RouteTable.Routes.MapHttpRoute("WebApi1", 
            "api/{controller}/{id}", 
            defaults:=New With {.id = System.Web.Http.RouteParameter.Optional}) 
End Sub