post and get with same method signature

前端 未结 7 1898
闹比i
闹比i 2020-12-02 15:39

In my controller I have two actions called \"Friends\". The one that executes depends on whether or not it\'s a \"get\" versus a \"post\".

So my code snippets look s

7条回答
  •  有刺的猬
    2020-12-02 15:49

    Rename the second method to something else like "Friends_Post" and then you can add [ActionName("Friends")] attribute to the second one. So the requests to the Friend action with POST as request type, will be handled by that action.

    // Get:
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Friends()
    {
        // do some stuff
        return View();
    }
    
    // Post:
    [ActionName("Friends")]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Friends_Post()
    {
        // do some stuff
        return View();
    }
    

提交回复
热议问题