using Apewer.Web; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Apewer.AspNetBridge { /// public abstract class ApiController { /// public ApiRequest Request { get; internal set; } /// public ApiResponse Response { get; internal set; } /// protected virtual IHttpActionResult Bytes(byte[] bytes, string contentType = "application/octet-stream", string attachmentName = null) { var har = new HttpActionResult(); har.Bytes = bytes; har.ContentType = contentType; har.Attachment = attachmentName; return har; } /// protected virtual IHttpActionResult Text(string text, string contentType = "text/plain") { var har = new HttpActionResult(); har.Bytes = text.Bytes(); har.ContentType = contentType; return har; } /// protected virtual IHttpActionResult Json(T content) { var json = Apewer.Json.From(content, false, -1, true); var text = json == null ? "" : json.ToString(); return Bytes(text.Bytes(), "application/json"); } /// protected static string MapPath(string relativePath) { var root = RuntimeUtility.ApplicationPath; var path = root; if (relativePath.NotEmpty()) { var split = relativePath.Split('/'); foreach (var seg in split) { if (seg.IsEmpty()) continue; if (seg == "~") path = root; path = Path.Combine(path, seg); } } return path; } } }