ModelBinder模型绑定扩展绑定cookie

public class CustomObjectModelBinder : IModelBinder {

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
        HttpCookie c = controllerContext.HttpContext.Request.Cookies["foo"]

        CustomObject value = new CustomObject() {
            foo.Name = c.Values["Name"],
            foo.Rank = c.Values["Rank"]
        }

        return CustomObject
    }

}

然后在 Application_Start()加上

ModelBinders.Binders.Add(typeof(CustomObject), new CustomObjectModelBinder());
posted @ 2012-08-17 11:59  我思故我在...  阅读(152)  评论(0编辑  收藏  举报