/// <summary> /// Returns the preferred content type to use for the response, based on the request, according to the following /// rules: /// 1. If the query string contains a key called "format", its value is returned as the content type /// 2. Otherwise, if the request has an Accepts header, the list of content types in order of preference is returned /// 3. Otherwise, if the request has a content type, its value is returned /// </summary> /// <param name="request"></param> /// <returns></returns> internal static List <ContentType> GetResponseFormats(HttpRequestBase request) { List <ContentType> result = DefaultFormatHelper.GetResponseFormatsCore(request); if (result == null) { ContentType contentType = DefaultFormatHelper.GetRequestFormat(request, true); result = new List <ContentType>(new ContentType[] { contentType }); } return(result); }
// Route-based format override so clients can use a route variable static bool TryGetFromRouteData(RouteData routeData, out ContentType contentType) { contentType = null; if (routeData != null) { string fromRouteData = routeData.Values[DefaultFormatHelper.formatVariableName] as string; if (!string.IsNullOrEmpty(fromRouteData)) { contentType = DefaultFormatHelper.ParseContentType(fromRouteData); } } return(contentType != null); }
public override IEnumerable <ContentType> GetResponseFormats(RequestContext requestContext) { IEnumerable <ContentType> result; if (!requestContext.HttpContext.Items.Contains(ResponseFormatKey)) { result = DefaultFormatHelper.GetResponseFormatsRouteAware(requestContext); requestContext.HttpContext.Items.Add(ResponseFormatKey, result); } else { result = (IEnumerable <ContentType>)requestContext.HttpContext.Items[ResponseFormatKey]; } return(result); }
/// <summary> /// Returns the format of a given request, according to the following /// rules: /// 1. If a Content-Type header exists it returns a ContentType for it or fails if one can't be created /// 2. Otherwie, if a Content-Type header does not exists it provides the default ContentType of "application/octet-stream" (per RFC 2616 7.2.1) /// </summary> /// <param name="request">The request.</param> /// <returns>The format of the request.</returns> /// <exception cref="HttpException">If the format is unrecognized or not supported.</exception> public override ContentType GetRequestFormat(RequestContext requestContext) { ContentType result; if (!requestContext.HttpContext.Items.Contains(RequestFormatKey)) { result = DefaultFormatHelper.GetRequestFormat(requestContext.HttpContext.Request, true); requestContext.HttpContext.Items.Add(RequestFormatKey, result); } else { result = (ContentType)requestContext.HttpContext.Items[RequestFormatKey]; } return(result); }
static List <ContentType> GetResponseFormatsCore(HttpRequestBase request) { ContentType contentType; if (DefaultFormatHelper.TryGetFromUri(request, out contentType)) { return(new List <ContentType>(new ContentType[] { contentType })); } string[] accepts = request.AcceptTypes; if (accepts != null && accepts.Length > 0) { return(DefaultFormatHelper.GetAcceptHeaderElements(accepts)); } return(null); }
static List <ContentType> GetResponseFormatsRouteAware(RequestContext requestContext) { List <ContentType> result = DefaultFormatHelper.GetResponseFormatsCore(requestContext.HttpContext.Request); ContentType contentType; if (result == null) { contentType = FormatManager.Current.FormatHelper.GetRequestFormat(requestContext); result = new List <ContentType>(new ContentType[] { contentType }); } if (TryGetFromRouteData(requestContext.RouteData, out contentType)) { result.Insert(0, contentType); } return(result); }
/// <summary> /// Determines whether the specified HTTP request was sent by a Browser. /// A request is considered to be from the browser if: /// it's a GET or POST /// and does not have a non-HTML entity format (XML/JSON) /// and has a known User-Agent header (as determined by the request's BrowserCapabilities property), /// </summary> /// <param name="request">The request.</param> /// <returns>true if the specified HTTP request is a Browser request; otherwise, false.</returns> internal static bool IsBrowserRequest(HttpRequestBase request) { if (!request.IsHttpMethod(HttpVerbs.Get) && !request.IsHttpMethod(HttpVerbs.Post)) { return(false); } ContentType requestFormat = DefaultFormatHelper.GetRequestFormat(request, false); if (requestFormat == null || string.Compare(requestFormat.MediaType, FormatManager.UrlEncoded, StringComparison.OrdinalIgnoreCase) != 0) { if (FormatManager.Current.CanDeserialize(requestFormat)) { return(false); } } HttpBrowserCapabilitiesBase browserCapabilities = request.Browser; if (browserCapabilities != null && !string.IsNullOrEmpty(request.Browser.Browser) && request.Browser.Browser != "Unknown") { return(true); } return(false); }
public static bool IsBrowserRequest(this HttpRequestBase request) { return(DefaultFormatHelper.IsBrowserRequest(request)); }
public static List <ContentType> GetResponseFormats(this HttpRequestBase request) { return(DefaultFormatHelper.GetResponseFormats(request)); }
public static ContentType GetRequestFormat(this HttpRequestBase request) { return(DefaultFormatHelper.GetRequestFormat(request, true)); }
public override bool IsBrowserRequest(RequestContext requestContext) { return(DefaultFormatHelper.IsBrowserRequest(requestContext.HttpContext.Request)); }