2

编辑:根据 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
4

1 回答 1

2

在 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.Netsintaxe,在以下代码中看起来相同C#

config.Routes.MapHttpRoute("DefaultApiGet",
                           "api/{controller}",
                           new {action = "Get"},
                           new {httpMethod = new HttpMethodConstraint(HttpMethod.Get)});

config.Routes.MapHttpRoute("DefaultApiWithAction",
                           "api/{controller}/{action}");
于 2013-07-29T19:04:18.687 回答