使用Web-API的MVC路由(404错误)?

时间:2012-06-12 19:08:11

标签: asp.net-mvc routing asp.net-web-api

我有一点时间弄清楚为什么我的路由配置不起作用。我尝试了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

1 个答案:

答案 0 :(得分: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

注意注册web api控制器的routes.MapHttpRoute