public HttpResponseMessage GetGeoPointWithValueProvider([ValueProvider(typeof(CookieValueProviderFactory))] GeoPointWithModelBinderAttribute location) { var response = Request.CreateResponse(HttpStatusCode.OK, location); CookieHeaderValue cookie = new CookieHeaderValue("location", 47.678558 + "," + -122.130989); cookie.Expires = DateTimeOffset.Now.AddDays(2); cookie.Domain = Request.RequestUri.Host; cookie.Path = "/"; response.Headers.AddCookies(new CookieHeaderValue[] { cookie }); return(response); }
static GeoPointModelBinder() { _locations["redmond"] = new GeoPointWithModelBinderAttribute() { Latitude = 47.67856, Longitude = -122.131 }; _locations["paris"] = new GeoPointWithModelBinderAttribute() { Latitude = 48.856930, Longitude = 2.3412 }; _locations["tokyo"] = new GeoPointWithModelBinderAttribute() { Latitude = 35.683208, Longitude = 139.80894 }; }
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(GeoPointWithModelBinderAttribute)) { return(false); } ValueProviderResult val = bindingContext.ValueProvider.GetValue( bindingContext.ModelName); if (val == null) { return(false); } string key = val.RawValue as string; if (key == null) { bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Wrong value type"); return(false); } GeoPointWithModelBinderAttribute result; if (_locations.TryGetValue(key, out result) || GeoPointWithModelBinderAttribute.TryParse(key, out result)) { bindingContext.Model = result; return(true); } bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Cannot convert value to GeoPoint"); return(false); }
public HttpResponseMessage GetGeoPointWithModelBinderAndProvider([ModelBinder] GeoPointWithModelBinderAttribute location) { return(Request.CreateResponse(HttpStatusCode.OK, location)); }