onmyway
onmyway

Reputation: 1515

AngularJS API Call with Id as param and C# Contoller

I hope you can help me. I feel like pulling my hair out!

I am trying to make an API call with an Id as the passed parameter to find a specific record.

Something like this: $http.get("/api/vendor/ReadVendor/" + vendorId)

What needs to happen:

When I select a Vendor the call to the API is made with the unique Vendor id. This is passed to the Controller and DTO and DB, and return the specified data.

I get an error:

404/Not Found {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:60090/api/vendor/ReadVendor/5'.","MessageDetail":"No action was found on the controller 'Vendor' that matches the request."}

My stack flow is something like this:

  1. adminVendorNumber.html
  2. adminVendorNumber.controller.js
  3. VendorController.cs
  4. VendorDTO.cs

Here is part of my adminVendorNumber.html used for the call to the AngularJS function:

                        <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>

This is my AngularJS controller (part of it with my specific function):

(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);
            }
        }

    };
}
)();

Here is part of my VendorController.cs function that is called:

    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);
            }
        }

    }
}

Here is the next step where the VedorDTO.cs is called:

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;
    }

Upvotes: 0

Views: 79

Answers (4)

Babu Swami
Babu Swami

Reputation: 798

It seems you have some issues with your API Controller. You have to define the HTTP Methods and routes in the controller to make it work. Also, you need to change the vendorId to 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
}

Upvotes: 0

Hassan Tariq
Hassan Tariq

Reputation: 740

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

and One more thing Try this for your get statment

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

Upvotes: 1

Jainish Jariwala
Jainish Jariwala

Reputation: 308

Please change your method name with GetReadVendor

OR

add HttpGet above your method as following :

[HttpGet]
public HttpResponseMessage ReadVendor(int vendorId)

Upvotes: 1

M.S.
M.S.

Reputation: 4423

Specify [HttpGet] attribute on the action as mentioned below:

[HttpGet]
public HttpResponseMessage ReadVendor(int vendorId)

Upvotes: 2

Related Questions