WebApi - Bind from both Uri and Body

前端 未结 4 1546
小鲜肉
小鲜肉 2020-12-13 12:51

Is it possible to bind a model from both the Uri and Body?

For instance, given the following:

routes.MapHttpRoute(
    name: \"API Default\",
    rou         


        
4条回答
  •  悲哀的现实
    2020-12-13 13:11

    If I understood you, this should work out of the box, e.g. this works for me:

        [HttpPost]
        public ActionResult Test(TempModel model)
        {
            ViewBag.Message = "Test: " + model.Id +", " + model.Name;
    
            return View("About");
        }
    
    public class TempModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    

    and on the request: localhost:56329/Home/Test/22 with body:{"Name":"tool"}

    I have my model's properties set accordingly to 22 and "tool".

提交回复
热议问题