/// <summary> /// Serializes the object into an action result based on the content type requested by the /// user (defaults to json) /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="Object">Object to serialize</param> /// <param name="ContentType">Content type to serialize it as</param> /// <returns>Action result</returns> protected virtual ActionResult Serialize <T>(T Object, string ContentType = "") { Utilities.IO.Serializers.Manager Manager = Utilities.IoC.Manager.Bootstrapper == null ? null : Utilities.IoC.Manager.Bootstrapper.Resolve <Utilities.IO.Serializers.Manager>(); var Result = new ContentResult(); if (Manager == null) { return(Result); } if (string.IsNullOrEmpty(ContentType)) { if (Request.AcceptTypes != null) { ContentType = Request.AcceptTypes.Length > 0 ? Request.AcceptTypes.FirstOrDefault(Manager.CanSerialize) : ""; } if (string.IsNullOrEmpty(ContentType) && Request.Path.Contains('.')) { ContentType = Manager.FileTypeToContentType(Request.Path.ToUpperInvariant().Right((Request.Path.Length - Request.Path.LastIndexOf('.')))); } if (string.IsNullOrEmpty(ContentType)) { ContentType = "application/json"; } } Result.Content = Object.Serialize <string, T>(ContentType); Result.ContentType = ContentType; return(Result); }
public void FileTypeToContentType() { var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects <ISerializer>()); Assert.Equal("application/json", Temp.FileTypeToContentType(".json")); }
public void FileTypeToContentType() { var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects<ISerializer>()); Assert.Equal("application/json", Temp.FileTypeToContentType(".json")); }