0

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

4 Answers 4

2

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

[HttpGet]
public HttpResponseMessage ReadVendor(int vendorId)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Very simple mistake :D
1

Please change your method name with GetReadVendor

OR

add HttpGet above your method as following :

[HttpGet]
public HttpResponseMessage ReadVendor(int vendorId)

Comments

1
[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)

1 Comment

Thanks for the response and help! Like this: $http.get("/api/vendor/ReadVendor/?vendorid=" + vendorId)
0

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
}

1 Comment

Thanks for your great response. This is obviously a coding pattern or technique? Where can I learn more about it? Also, is this an older coding technique, or newer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.