Question

I'm using an AngularJS $resource to contact MVC ActionMethods on the server. The resource URL is /api/customer, so for the $resource.query function I provide an ActionMethod called Customer on the ApiController. This works fine.
Now I want to provide an ActionMethod for $resource.save. Obviously, the 2nd ActionMethod needs the same name, but since neither ActionMethod has any parameters this is not allowed.
What is the best practice for this situation? Should I stay away from server side MVC for AngularJS projects? Thanks in advance.

Was it helpful?

Solution

You should create a controller CustomerController deriving from ApiController. Then the methods

Get(int id) works for getting customer with specific id. Url: api/customer/1

Delete(int id) works for deleting customer with specific id. Url: api/customer/1

OTHER TIPS

Further to my comment, I am adding this answer in case someone finds it helpful. It solves my original problem but I don't know if it's the best solution.

Original factory:

app.factory("customerResource", function ($resource) {
    var customerResource = $resource("/api/customer/:id", { id: "@id" });
    return customerResource;
});

Original line in controller:

$scope.customers = customerResource.query();

Amended factory:

app.factory("customerResource", function ($resource) {
    var customerResource = $resource("/api/:action/:id", { id: "@id", action: "@action" });
    return customerResource;
});

Amended line in controller:

$scope.customers = customerResource.query({action: "get");

Now I can use other $resource functions using a different action, like so:

drawing.$remove({action: "delete"});

These calls will reach the Get and Delete ActionMethods on the ASP.MVC server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top