编辑:PER Felipe的回答,我将webapiconfig中的代码更改为以下内容,并且效果很好 -
config.Routes.MapHttpRoute( _
name:="DefaultApi", _
routeTemplate:="api/{controller}/{action}/{id}", _
defaults:=New With {.id = RouteParameter.Optional} _
)
config.Routes.MapHttpRoute(name:="API Default", routeTemplate:="api/{controller}/{action}/{id}", defaults:=New With { _
.id = RouteParameter.[Optional] _
})
我有一个带有4个功能的web api控制器 -
<HttpGet> _
<ActionName("AllCerts")> _
Public Function GetCerts() As Object
Dim LWCERTS As Array = objCert.GetCertificates
Return LWCERTS
End Function
<HttpGet> _
<ActionName("MyCerts")> _
Public Function GetMyCert() As Object
Dim lwMyCerts As Array = objCert.GetCertificates(Utilities.GetLogin())
Return lwMyCerts
End Function
<HttpGet> _
<ActionName("GetValueDDA")> _
Public Function GetDDABanks()
Dim objDDABankNum As New LucasEntities.Business.EF_DDA
Dim lwDDABankNum As Array = objDDABankNum.GetDDABankJSON()
Return lwDDABankNum
End Function
'' POST api/caapproval
<HttpPost> _
<ActionName("CertDtlsByID")> _
Public Function Post(value As CertDetailModel) As Object
Dim objCertPosting As New LucasEntities.Business.EF_CertificatePosting
Dim lwMyCertDetails As String = objCertPosting.GetBorrowingBaseAdvanceRequestJSON()
Return lwMyCertDetails
End Function
在我的数据服务中,我有以下ajax调用 -
var getallCertificates = function (CertificatesObservable) {
var dataObservableArray = ko.observableArray([]);
var newJ;
$.ajax({
type: "GET",
dataType: "json",
url: "/api/caapproval/AllCerts/",
async: false,
success: function (dataIn) {
newJ = $.parseJSON(dataIn);
CertificatesObservable([]);
dataIn.forEach(function (p) {
var certificate = new certificateModel(p.clientID, p.lwCertID, p.requestDate, p.userName, p.statusDescription, p.statusCode, p.statusDesc, p.ceoUserName, p.clientName, p.clientNumber, p.borrowBaseCount, p.advRequestCount, p.certType);
CertificatesObservable.push(certificate);
});
return CertificatesObservable(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
//jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
return CertificatesObservable(newJ);
}
这是我的RouteConfig类 -
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
End Sub
结束班
当我在控制器中放置断点时,我看到CertDtlsByID actionName下的Post函数始终运行,而不是AllCerts ActionName下的GetCerts。作为web api和路由的新手,我做错了什么导致总是调用“Post”方法?
当我直接在浏览器中运行网络API时,Chrome会显示以下错误 -
Multiple actions were found that match the request: System.Object GetCerts() on type LucasNetApp.CAApprovalController System.Object GetMyCert() on type LucasNetApp.CAApprovalController System.Object GetDDABanks() on type LucasNetApp.CAApprovalController
答案 0 :(得分:3)
在asp.net web api中,路由由WebApiConfig.cs
文件夹上的App_Start
文件控制。
ActionName
属性不起作用,因为它是asp.net mvc的行为。
打开WebApiConfig.cs
文件并尝试添加这些路由配置:
config.Routes.MapHttpRoute(name:="DefaultApiGet",
url:="api/{controller}",
defaults:=New With {.action = "Get"},
constraints:=new With {.httpMethod = new HttpMethodConstraint(HttpMethod.Get)})
config.Routes.MapHttpRoute(name:="DefaultApiWithAction",
url:="api/{controller}/{action}")
我不确定Vb.Net
sintaxe,C#
中的代码相同:
config.Routes.MapHttpRoute("DefaultApiGet",
"api/{controller}",
new {action = "Get"},
new {httpMethod = new HttpMethodConstraint(HttpMethod.Get)});
config.Routes.MapHttpRoute("DefaultApiWithAction",
"api/{controller}/{action}");