开发者

Overloading Controller methods with custom JSON(POST) binding in MVC3

开发者 https://www.devze.com 2023-02-19 08:08 出处:网络
I reached a road bump while trying to use some of the cool new feature of MVC 3. Is it possible to overload controllers using custom JSON binding using MVC3 ?

I reached a road bump while trying to use some of the cool new feature of MVC 3.

Is it possible to overload controllers using custom JSON binding using MVC3 ?

It doesnt look like it works automatically as of now.... What's the neatest way to do this ? For example If I want to implement the following endpoint

[HttpPost]
public ActionResult GetPet(Cat catObject)
{
return Json(catObject.purr());
}

overloaded with this endpoint

[HttpPost]
public ActionResult GetPet(Dog dogObject)
{
return Json(dog.bark());
}

Is there any way i can do this without using thirdparty libr开发者_如何学Goaries Or System.Web.Script.Serialization.JavaScriptSerializer

Also Is there any particular reason this is not implemented in mvc3 yet?


Overloading Json objects on controllers seems a distant possibility now rather than a present reality. This is the closest I could get to overloading the action

[HttpPost]
public ActionResult GetPet()
{
Cat catObj;
Dog dogObg;
if (TryUpdateModel(catObj))
        return Json(catObj.purr());
else
{
    ModelState.Clear();
    if (TryUpdateModel(dogObg))
        return Json(dogObj.bark());
    else
    {
        ModelState.Clear();
        ModelState.AddModelError("InvalidInput", "The given input does not match with any of the accepted JSON types");
        return new HttpBadRequestResult(ModelState);
    }
}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号