AngularJS API调用ID为param和C#Contoller

时间:2016-03-30 12:26:13

标签: c# html angularjs

我希望你能帮助我。我想把头发拉出来!

我正在尝试使用Id作为传递参数进行API调用以查找特定记录。

这样的事情:$http.get("/api/vendor/ReadVendor/" + vendorId)

需要做什么:

当我选择供应商时,使用唯一的供应商ID进行API调用。这将传递给Controller和DTO以及DB,并返回指定的数据。

我收到错误:

404 / Not Found {" Message":"未找到与请求URI匹配的HTTP资源' http://localhost:60090/api/vendor/ReadVendor/5' 。"," MessageDetail":"未在控制器'供应商'上找到任何操作。与请求匹配。" }

我的堆栈流程是这样的:

  1. adminVendorNumber.html
  2. adminVendorNumber.controller.js
  3. VendorController.cs
  4. VendorDTO.cs
  5. 以下是用于调用AngularJS函数的adminVendorNumber.html的一部分:

                            <label>Vendor</label>
                            <div class="input-dropdown">
                                <cc-dropdown cc-placeholder="Select Vendor"
                                             ng-model="NewVendorNumber.Vendor"
                                             ng-disabled="false"
                                             ng-options="vendorData"
                                             cc-fields="VendorId"
                                             cc-key-field="VendorId"
                                             cc-allow-search="false"
                                             ng-required="false"
                                             ng-change="vendorSelected()"
                                             name="iVendor">
                                </cc-dropdown>
                            </div>
                        </div>
    

    这是我的AngularJS控制器(具有我特定功能的部分):

    (function () {
        "use strict";
    
        angular
            .module('app.adminVendorNumber')
            .controller('adminVendorNumberController', adminVendorNumberController);
    
        adminVendorNumberController.$inject = ['$http', 'logger', '$scope'];
    
        function adminVendorNumberController($http, logger, $scope) {
            var vm = $scope;
            vm.formSubmmision = true;
    
            vm.vendorItemData = null;
            vm.itemGroupData = null;
            vm.vendorData = null;
    
            vm.vendorSelected = vendorSelected;
    
            vm.save = save;
    
            activate();
    
            function activate() {
                return vendorItemData().then(getAllItemGroups).then(getVendorData).then(function () {
                    logger.info('Activated Vendor Number Creation');
                });
            }
    
    
            function vendorSelected() {
    
                vm.formSubmmision = true;
                return getVendorById(vm.NewVendorNumber.Vendor.VendorId);
            }
    
            function getVendorById(vendorId) {
                return $http.get("/api/vendor/ReadVendor/" + vendorId)
                    .then(Success)
                    .catch(Failure);
    
                function Success(responce) {
                    vm.vendorSelected = responce.data.Records;
                    return vm.vendorSelected;
                }
    
                function Failure(error) {
                    logger.error('Failed to get Vendor Data ' + error.data.Message);
                }
            }
    
        };
    }
    )();
    

    以下是我的VendorController.cs函数的一部分:

        public class VendorController : ApiController
        {
            private VendorDTO dto = new VendorDTO();
    
            public HttpResponseMessage ReadVendor(int vendorId)
            {
                try
                {
                    CommandResult<Vendor> result = dto.ReadVendor(vendorId);
                    return Request.CreateResponse((result.Status == CommandStatus.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError), result);
                }
                catch (IOWAException ex)
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
                }
            }
    
        }
    }
    

    以下是调用VedorDTO.cs的下一步:

    public class VendorDTO
    {
    
        public CommandResult<Vendor> ReadVendor(int vendorId)
        {
            return readVendor(vendorId);
        }
    
        private CommandResult<Vendor> readVendor(int vendorId)
        {
            CommandResult<Vendor> result = null;
    
            try
            {
                VendorContext dalVendor = new VendorContext();
                result = dalVendor.Read(vendorId);
            }
            catch (IOWAException ex)
            {
                result = new CommandResult<Vendor>(ex, "Error reading Vendors");
            }
    
            return result;
        }
    

4 个答案:

答案 0 :(得分:2)

如下所述,在动作上指定[HttpGet]属性:

[HttpGet]
public HttpResponseMessage ReadVendor(int vendorId)

答案 1 :(得分:1)

请使用 GetReadVendor

更改您的方法名称

在方法上方添加HttpGet,如下所示:

# Using mlply to run the function, and find the closest coordinates with progress bar
library(plyr)
postcode[mlply(cbind(x1, y1), .fun = algo, .progress = "tk")]

答案 2 :(得分:1)

[HttpGet]
 public CommandResult<Vendor> ReadVendor(int vendorId)
{
    return readVendor(vendorId);
}

还有一件事 试试这个获得的陈述

         $http.get('Your URL/?vendorId='+your val)

答案 3 :(得分:0)

您的API控制器似乎存在一些问题。您必须在控制器中定义HTTP方法和路由以使其工作。此外,您需要将vendorId更改为id。

[RoutePrefix("api/vendor")]
public class VendorController : ApiController
{
    [Route("ReadVendor"), HttpGet]
    public HttpResponseMessage ReadVendor(int? id)
    {
        return Request.CreateResponse(HttpStatusCode.OK, "");
    }
    //Rest of your code
}