1

我花了很长时间弄清楚为什么我的路由配置不起作用。我尝试了 mysite/issues/500 和 mysite/issues/ 但我总是收到 404 错误。我在两个 get 函数中都放了一个断点,它永远不会到达断点。想也许一瞥可以帮助我,但我试图

这是我的 global.asax:

    Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Sub Application_Start()
        AreaRegistration.RegisterAllAreas()
        RouteTable.Routes.MapRoute("Issues", "{controller}/{id}", New With {.id = UrlParameter.Optional})
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
    End Sub
End Class

控制器:

Namespace Controllers


    Public Class IssuesController
        Inherits ApiController

        ' GET api/issues'
        Public Function GetValues(ByVal req As HttpRequestmessage) As IEnumerable(Of Issue)
            Dim querystrParams As NameValueCollection = Nothing
            Dim srch As IQuery
            If req.RequestUri.Query.Any() Then
                srch = New IssueTrackerQuery(req.RequestUri.ParseQueryString())
            End If
            Return (New EnumerableQuery(Of Issue)(New HashSet(Of Issue)))
        End Function

        ' GET api/issues/5'
        Public Function GetValue(ByVal IssuesId As Integer) As Issue
            Return New Issue()
        End Function

        ' POST api/issues'
        Public Sub PostValue(ByVal value As String)
            Throw New NotImplementedException()
        End Sub

        ' PUT api/issues/5'
        Public Sub PutValue(ByVal id As Integer, ByVal value As String)
            Throw New NotImplementedException()
        End Sub

        ' DELETE api/issues/5'
        Public Sub DeleteValue(ByVal IssuesId As Integer)
            Throw New NotImplementedException()
        End Sub

    End Class
End Namespace
4

1 回答 1

4

您的 Global.asax 中没有任何 API 路由。MapHttpRoute使用扩展方法添加一个(而不是MapRoute):

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()
    RouteTable.Routes.MapHttpRoute("Issues", "{controller}/{id}", New With {.id = UrlParameter.Optional})
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
    RouteConfig.RegisterRoutes(RouteTable.Routes)
End Sub

注意routes.MapHttpRoute哪个注册了 web api 控制器。

于 2012-06-12T19:10:55.207 回答