编辑:根据 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 和路由的新手,我做错了什么导致“发布”方法总是被调用?
当我直接在浏览器中运行 web 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