【问题标题】:VB.Net Web Api Action not invoked未调用 VB.Net Web Api 操作
【发布时间】:2014-06-23 19:17:57
【问题描述】:

我有尝试调用的 vb.net web api 控制器,但我得到以下内容:

 {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:26944/api/employee/GetPerson/'.","MessageDetail":"No action was found on the controller 'Employee' that matches the request."}

这是网络控制器:

Public Class EmployeeController
    Inherits ApiController
    Private ReadOnly dbContext As MyEntities

    Sub New()
        Me.dbContext = New MyEntities
    End Sub

    <HttpGet>
    <ActionName("GetPerson")>
    Function Person(ByVal missionaryId As Integer) As IPRS_Data.getPersInfoDetail_Result
        Return Me.dbContext.getPersInfoDetail(missionaryId).First
    End Function

End Class

WebApiConfig:

Public Shared Sub Register(ByVal config As HttpConfiguration)
    ' Web API configuration and services

    ' Web API routes
    config.MapHttpAttributeRoutes()

    config.Routes.MapHttpRoute(
        name:="DefaultApi",
        routeTemplate:="api/{controller}/{id}",
        defaults:=New With {.id = RouteParameter.Optional}
    )
    Dim xmlFormat = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(Function(t) t.MediaType = "application/xml")
    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(xmlFormat)
End Sub

我正在使用以下方式调用服务:appbase/api/employee/GetPerson/

【问题讨论】:

    标签: asp.net vb.net web-services


    【解决方案1】:

    你的方法用 HTTPGET 和 Actionname 属性修饰。如果您的方法名称以“Get”开头(如 GetPerson),则不需要它。但是,Actionname 已过时,因为在您的实际路由中没有考虑它。您的路由是"api/{controller}/{id}"。如果您希望考虑您的操作名称,您需要将您的路由修改为"api/{controller}/{action}/{id}"。如果你想让你的 id-param 在默认路由中被考虑,你应该将方法中的参数从 missionaryId 重命名为 id

    Function Person(ByVal id As Integer) As IPRS_Data.getPersInfoDetail_Result
        Return Me.dbContext.getPersInfoDetail(id).First
    End Function
    

    这就是调用它的方式(不要忘记传递一个 ID,因为您的控制器中没有其他无参数的“GET”方法。

    appbase/api/employee/15
    

    appbase/api/employee?id=15
    

    如果你坚持要传教士身份

    appbase/api/employee?missionaryId=15
    

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2014-10-14
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多