MVC Web API posting JSON data gives me a 404

前端 未结 2 1675
没有蜡笔的小新
没有蜡笔的小新 2021-01-25 09:33

I have a Web API in MVC4. I\'m getting a 404 when posting data using ajax and I don\'t understand why.

LanguageController:

[AcceptVerbs(\"POST\")]
public         


        
2条回答
  •  醉话见心
    2021-01-25 09:51

    Cuong Le's answer is the correct way make delete requests, however it wasn't an answer to my question. This was more to do with working out posting data to the server with MVC and Angular so the delete action was immaterial.

    The mistake I was making was assuming that MVC's routing would pull the ID from the posted data.

    What I did was add my Language model as a parameter on my method:

    [AcceptVerbs("POST")]
    public void Delete(Language lang)
    {    
        Language language = db.Languages.Find(lang.ID);
        db.Languages.Remove(language);
        db.SaveChanges();    
    }
    

    This solves my 404 and leaves me feeling rather stupid as it's pretty obvious now that the routing wouldn't just know to pull the ID out... I could probably improve it by using a view model instead of my Language model.

提交回复
热议问题