1

I'm new to using web api.. I am using some angular code. Adding and listing data works fine, but not deleting it. I tried a lot of stuff from stackoverflow add web configuration, I have my cors set to allow all site since this is just for testing.

Here's my code.. I don't what I'm missing

[HttpDelete]       // I also tried HttpPut 
[Route("products/RemProduct")]
public string RemoveProduct(int prodId)
{
    if (prodId != null)
    {
        using (ProductContext dataContext = new ProductContext())  // I tried removing this using
        {
            // this was the my original code
            //var removeProd = new ProductTest { Id = prodId };
            //dataContext.ProductData.Remove(removeProd);

            // used this as my alternative
            dataContext.Database.ExecuteSqlCommand("DELETE FROM ProductTest where Id = " + prodId); 

            //dataContext.SaveChanges();
            return "Product Removed";
        }
    }

    return "Invalid";
}

This is the angular service part

    this.DeleteProd = function (prodId) {
    var response = $http({
        method: "DELETE",
        url: "https://apirepo.leofaj.org/products/RemProduct", 
         params: {
             Id: prodId
         },
      //  data:prodId,
        //dataType:"json"
    });
    return response;
}

This is the web config that I tried

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">  <-- added this too.
    </modules>
    <security>
        <requestFiltering>
            <verbs>
                <add verb="Delete" allowed="true" /> <<-- added this
            </verbs>
        </requestFiltering>
    </security>
</system.webServer>

This is the error I'm getting

DELETE https://apirepo.leofaj.org/products/RemProduct?Id=13 404 ()

I really don't know why adding data is working with put then I tried put request and using the raw sql..this runs on local server.. 2 separate iis website.

I tried the solution posted from the suggested duplicate you can see it from my example..the answer to my question is because I didnt know that i have to pass same param name as my action param.

Thanks

CyberNinja
  • 872
  • 9
  • 25
  • 2
    Possible duplicate of [Entity Framework: How do I delete a record by its primary key?](http://stackoverflow.com/questions/8391520/entity-framework-how-do-i-delete-a-record-by-its-primary-key) – ste-fu Nov 01 '16 at 13:06
  • OT `if (prodId != null)` - this will always be true, an `int` cannot be null. – Hans Kesting Nov 01 '16 at 13:07
  • @ste-fu check my my answer..i used that solution..my problem was it is still not working. – CyberNinja Nov 01 '16 at 13:15

3 Answers3

4

The query parameter should be named exactly as the action's argument.

url: "https://apirepo.leofaj.org/products/RemProduct", 
params: {
    prodId: prodId      // <-- Change this
},
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
1

You need to track the entry which you want to remove, so try using this

using (ProductContext dataContext = new ProductContext())
            {
                var removeProd = dataContext.ProductData.Where(a => a.Id == prodId).FirstOrDefault();
                dataContext.ProductData.Remove(removeProd);

                dataContext.SaveChanges();
                return "Product Removed";
            }

also query parameter need to be matched

this.DeleteProd = function (prodId) {
    var response = $http({
        method: "DELETE",
        url: "https://apirepo.leofaj.org/products/RemProduct", 
         params: {
             prodId: prodId
         },
      //  data:prodId,
        //dataType:"json"
    });
    return response;
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
0

Use the same params you used on your controller/api

Like the answer above pass prodId as your variable then the source value won't matter.