Last active
August 29, 2015 14:22
-
-
Save rorymurphy/018d75fda12567f7fc9c to your computer and use it in GitHub Desktop.
ASP.NET MVC Multi-format responder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Copyright 2015 Rory A. Murphy | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Web.Mvc; | |
| namespace System.Web.Mvc | |
| { | |
| public enum ContentType {HTML, JSON, XML, RSS, ATOM}; | |
| public class XFormatResponder | |
| { | |
| public static readonly Dictionary<string, string> TYPE_NAME_MAPPINGS = new Dictionary<string, string>() { | |
| {"text/html", ContentType.HTML.ToString()}, | |
| {"application/json", ContentType.JSON.ToString()}, | |
| {"application/xml", ContentType.XML.ToString()} | |
| }; | |
| public XFormatResponder() | |
| { | |
| this._responders = new Dictionary<string, Func<ActionResult>>(); | |
| this._preferenceOrder = new List<string>(); | |
| } | |
| private Dictionary<string, Func<ActionResult>> _responders; | |
| private List<string> _preferenceOrder; | |
| public Func<ActionResult> this[ContentType contentType] | |
| { | |
| get | |
| { | |
| return this[contentType.ToString()]; | |
| } | |
| set | |
| { | |
| this[contentType.ToString()] = value; | |
| } | |
| } | |
| public Func<ActionResult> this[string contentType] | |
| { | |
| get | |
| { | |
| return this._responders[contentType]; | |
| } | |
| set | |
| { | |
| this._responders[contentType] = value; | |
| this._preferenceOrder.Add(contentType); | |
| } | |
| } | |
| internal ActionResult Respond(System.Web.HttpRequestBase request) | |
| { | |
| if(this._preferenceOrder.Count == 0){return null;} | |
| Func<ActionResult> result = null; | |
| SingleValueEventArgs<IDictionary<string, string>> mappings = new SingleValueEventArgs<IDictionary<string, string>>() | |
| { | |
| Value = new Dictionary<string, string>(TYPE_NAME_MAPPINGS) | |
| }; | |
| foreach(string aType in request.AcceptTypes){ | |
| if ("*/*" == aType) { | |
| result = this._responders[this._preferenceOrder[0]]; | |
| break; | |
| } | |
| else if (mappings.Value.ContainsKey(aType)) | |
| { | |
| string cType = mappings.Value[aType]; | |
| if (this._responders.ContainsKey(cType)) | |
| { | |
| result = this._responders[cType]; | |
| break; | |
| } | |
| } | |
| } | |
| if (result == null) { return null; } | |
| return result(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment