public ImageModel(string ImagePath, System.Web.Mvc.UrlHelper url) { dir = new DirectoryInfo(ImagePath); thumbDir = dir.GetDirectories("t").FirstOrDefault(); if (!dir.Exists) { dir.Create(); } if (thumbDir == null || !thumbDir.Exists) { thumbDir = dir.CreateSubdirectory("t"); } Images = from f in dir.GetFiles() let exists = File.Exists(Path.Combine(thumbDir.FullName, f.Name)) select new Img { Filename = f.Name, ImageUrl = url.Content("~/Images/" + f.Name), Uploaded = f.CreationTime, Size = f.Length, HasThumbnail = exists, ThumbnailUrl = exists ? url.Content("~/Images/t/" + f.Name) : null }; }
public static string Content(this System.Web.Mvc.UrlHelper Url, string Path, bool addTimeStamp) { if (!addTimeStamp) { return(Url.Content(Path)); } string serverPath = HttpContext.Current.Server.MapPath(Path); DateTime lastWrite = File.GetLastWriteTimeUtc(serverPath); string result = lastWrite.Ticks.ToString(); return(Url.Content(Path) + "?t=" + result); }
public static string Content(this System.Web.Mvc.UrlHelper urlHelper, string contentPath, bool toAbsolute = false) { var path = urlHelper.Content(contentPath); var url = new Uri(System.Web.HttpContext.Current.Request.Url, path); return(toAbsolute ? url.AbsoluteUri : path); }
/// <summary> /// 将虚拟(相对)路径转换为应用程序绝对路径 /// </summary> /// <param name="url">已呈现的页的 URL</param> /// <param name="contentPath">内容的虚拟路径</param> /// <param name="Version">版本号</param> /// <returns>应用程序绝对路径</returns> public static string Content2(this System.Web.Mvc.UrlHelper url, string contentPath, string Version) { // 防止低级错误 if (url == null) { throw new Exception("非网页请求不可调用此方法"); } if (string.IsNullOrEmpty(contentPath) || string.IsNullOrEmpty(Version)) { return(url.Content(contentPath)); } string cUrl = contentPath.ToLower(); // 给 js 和 css 文件加上版本号 if (cUrl.EndsWith(".js") || cUrl.EndsWith(".css")) { contentPath += "?" + Version; } return(url.Content(contentPath)); }
public string GetUserPic() { var jwtObject = GetjwtToken(); UserPic userPic = _userPicService.GetPicByAcc(jwtObject["Account"].ToString()); if (userPic != null) { System.Web.Mvc.UrlHelper urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext); return(urlHelper.Content("~/UserPic/" + userPic.Name)); } return(null); }
/// <summary> /// Address the specified urlHelper, contentPath and defaultValue. /// </summary> /// <param name="urlHelper">URL helper.</param> /// <param name="contentPath">Content path.</param> /// <param name="defaultValue">Default value.</param> public static string Addr(this System.Web.Mvc.UrlHelper urlHelper, string contentPath, string defaultValue = "") { if (string.IsNullOrEmpty(contentPath)) { return(defaultValue); } if (contentPath.StartsWith("~")) { return(urlHelper.Content(contentPath)); } return(contentPath); }
/// <summary> /// ResizeImage图片地址生成 /// </summary> /// <param name="url">图片地址</param> /// <param name="w">最大宽度</param> /// <param name="h">最大高度</param> /// <param name="quality">质量0~100</param> /// <param name="image">占位图类别</param> /// <returns>地址为空返回null</returns> public static string ResizeImage(string url, int?w = null, int?h = null, int?quality = null, Enums.DummyImage?image = Enums.DummyImage.Default, Enums.ResizerMode?mode = null, Enums.ReszieScale?scale = null ) { var Url = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext); if (string.IsNullOrEmpty(url)) { return(null); } else { if (Url.IsLocalUrl(url)) { var t = new Uri(HttpContext.Current.Request.Url, Url.Content(url)).AbsoluteUri; Dictionary <string, string> p = new Dictionary <string, string>(); if (w.HasValue) { p.Add("w", w.ToString()); } if (h.HasValue) { p.Add("h", h.ToString()); } if (scale.HasValue) { p.Add("scale", scale.Value.ToString()); } if (quality.HasValue) { p.Add("quality", quality.ToString()); } if (image.HasValue) { p.Add("404", image.ToString()); } if (mode.HasValue) { p.Add("mode", mode.ToString()); } return(t + p.ToParam("?")); } else { return(url); } } }
/// <summary> /// Essentially the same as Url.Content( ), but automatically prepends the '~' and '/' characters as needed /// </summary> /// <param name="urlHelper"></param> /// <param name="contentPathValue"></param> /// <returns></returns> public static String ContentFromRoot(this System.Web.Mvc.UrlHelper urlHelper, String contentPathValue) { String realPath = contentPathValue ?? String.Empty; if (!realPath.StartsWith("~")) { if (!realPath.StartsWith("/")) { realPath = "/" + realPath; } realPath = "~" + realPath; } return(urlHelper.Content(realPath)); }
/// <summary> /// Convert widget descriptor to dynamic object. /// </summary> /// <param name="httpContext">The http context.</param> /// <param name="website">The website name.</param> /// <param name="locale">The locale name.</param> /// <returns></returns> public dynamic ToObject(HttpContextBase httpContext, string website = "home", string locale = "en-US") { var Url = new System.Web.Mvc.UrlHelper(httpContext.Request.RequestContext); return(new { id = this.ID, title = this.Title, description = this.Description, icon = string.IsNullOrEmpty(this.IconUrl) ? null : Url.Content(this.IconUrl), contentUrl = GetContentUrl(httpContext, website, locale), contentType = this.ContentType, version = this.Version, defaults = this.Defaults }); }
public void SendOrderShippedEmail(Int64 OrderId, HttpRequestBase Request, System.Web.Mvc.UrlHelper Url, ApplicationUser user) { var order = new Order(); var orderDetails = order.GetOrderDetail(OrderId); var fileContents = System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/Content/Email/OrderShipped.html")); fileContents = fileContents.Replace("[#ORDERDATE#]", orderDetails.OrderDate.ToShortDateString()); fileContents = fileContents.Replace("[#ORDERNO#]", StringHelper.GetOrderNo(orderDetails.OrderId, orderDetails.OrderDate)); fileContents = fileContents.Replace("[#PAYMENT#]", orderDetails.Paid ? "PAID" : "NOT PAID"); fileContents = fileContents.Replace("[#ORDERTOTAL#]", "$" + orderDetails.TotalPrice.ToString()); fileContents = fileContents.Replace("[#SHIPPINGMETHOD#]", orderDetails.ShippingMethodName); fileContents = fileContents.Replace("[#SHIPPINGCODE#]", orderDetails.ShippingCode); string productsHtml = ""; foreach (var product in orderDetails.OrderProducts) { productsHtml += "<tr>"; productsHtml += "<td><img src=\"" + Request.Url.Scheme + "://" + Request.Url.Authority + Url.Content("~/ProductImage/" + product.FileName + "-1.jpg") + "\" /></td>"; productsHtml += "<td>" + product.VariantName + " " + product.ProductName + "</td>"; productsHtml += "<td>" + product.Quantity + "</td>"; productsHtml += "<td>" + product.UnitPrice + "</td>"; productsHtml += "<td>" + product.TotalPrice + "</td>"; productsHtml += "</tr>"; } fileContents = fileContents.Replace("[#PRODUCTS#]", productsHtml); var model = new EmailModel(); model.Body = fileContents; model.MailTo = user.Email; model.Title = "Order Shipped!"; string error = ""; SendEmail(model, ref error); }
/// <summary> /// 获取文件地址 /// </summary> /// <param name="fileId">文件名</param> /// <param name="filePath">FileConfig 对应的路径</param> /// <param name="httpContext">当前上下文 this.HttpContext</param> /// <returns>返回相对路径</returns> public string GetFileUrl(string fileId, string filePath, HttpContextBase httpContext) { string fileUrl = ""; if (!String.IsNullOrEmpty(fileId)) { var photo = String.Format("{0}/{1}/{2}", filePath, fileId.Substring(0, 4), fileId.Substring(4)); //var path = HttpContext.Current.Server.MapPath(photo); if (FileConfig.ResourceStatus == 1) { //七牛路径 fileUrl = String.Format("{0}/{1}/{2}/{3}", FileConfig.FileWebPath, filePath, fileId.Substring(0, 4), fileId.Substring(4)); } else { var url = new System.Web.Mvc.UrlHelper(httpContext.Request.RequestContext); fileUrl = url.Content(photo); } } return(fileUrl); }
public static string GetBaseURL(HttpRequestBase request) { System.Web.Mvc.UrlHelper url = new System.Web.Mvc.UrlHelper(request.RequestContext); return(string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, url.Content("~"))); }
/// <summary> /// 将虚拟(相对)路径转换为应用程序绝对路径 /// </summary> /// <param name="url">已呈现的页的 URL</param> /// <param name="contentPath">内容的虚拟路径</param> /// <returns>应用程序绝对路径</returns> public static string Content2(this System.Web.Mvc.UrlHelper url, string contentPath) { // 防止低级错误 if (url == null || HttpContext.Current == null) { throw new Exception("非http请求不可调用此方法"); } if (string.IsNullOrEmpty(contentPath)) { return(url.Content(contentPath)); } string lowerUrl = contentPath.ToLower(); // 给 js 和 css 文件加上版本号 if (lowerUrl.EndsWith(".js") || lowerUrl.EndsWith(".css")) { // 读取缓存的(本地的不缓存,以便调试) if (FileVersion.ContainsKey(lowerUrl) && IsLocal() == false) { return(FileVersion[lowerUrl]); } // 将虚拟路径,转换为物理路径 var path = HttpContext.Current.Server.MapPath(contentPath); var file = new System.IO.FileInfo(path); // 获取文件的最后修改时间 var Version = file.LastWriteTime.ToString("yyyyMMddHHmmss"); if (string.IsNullOrEmpty(Version)) { return(url.Content(contentPath)); } // 生成压缩的js文件 if (lowerUrl.EndsWith(".js") && !lowerUrl.EndsWith(".min.js")) { // 当配置了需要压缩js时,执行压缩。否则保持原文件 var needjsMin = Barfoo.Library.Configuration.ConfigurationUtility.AppSettings <int>("JsMin", 0) == 1; if (needjsMin) { // 保存原文件路径,压缩不成功时用原文件 var oldContent = contentPath; try { var filePath = file.FullName; // 生成压缩的js文件,结尾为".min.js",前台访问的是压缩的版本 var minName = filePath.Substring(0, filePath.Length - 3) + ".min.js"; new JsMin().Minify(filePath, minName); // 压缩成功,用压缩的js文件 contentPath = contentPath.Substring(0, contentPath.Length - 3) + ".min.js"; } // 文件压缩会出现异常,这时候用回原文件 catch { contentPath = oldContent; } } } // 给文件加上版本号 var newPath = url.Content(contentPath + "?" + Version); // 缓存起来 FileVersion[lowerUrl] = newPath; return(newPath); } return(url.Content(contentPath)); }
/// <summary> /// Returns style tags based on the webpack asset manifest /// </summary> /// <param name="htmlHelper"></param> /// <param name="urlHelper">Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file.</param> /// <returns></returns> public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null) { return(new HtmlString(string.Join("", Environment.GetStylePaths() .Select(stylePath => $"<link rel=\"stylesheet\" href=\"{(urlHelper == null ? stylePath : urlHelper.Content(stylePath))}\" />")))); }
/// <summary> /// Returns script tags based on the webpack asset manifest /// </summary> /// <param name="htmlHelper"></param> /// <param name="urlHelper">Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file.</param> /// <returns></returns> public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null) { string nonce = Environment.Configuration.ScriptNonceProvider != null ? $" nonce=\"{Environment.Configuration.ScriptNonceProvider()}\"" : ""; return(new HtmlString(string.Join("", Environment.GetScriptPaths() .Select(scriptPath => $"<script{nonce} src=\"{(urlHelper == null ? scriptPath : urlHelper.Content(scriptPath))}\"></script>")))); }
/// <summary> /// Convert widget descriptor to dynamic object. /// </summary> /// <param name="httpContext">The http context.</param> /// <param name="website">The website name.</param> /// <param name="locale">The locale name.</param> /// <returns></returns> public dynamic ToObject(HttpContextBase httpContext, string website = "home", string locale = "en-US") { var Url = new System.Web.Mvc.UrlHelper(httpContext.Request.RequestContext); return new { id = this.ID, title = this.Title, description = this.Description, icon = string.IsNullOrEmpty(this.IconUrl) ? null : Url.Content(this.IconUrl), contentUrl = GetContentUrl(httpContext, website, locale), contentType = this.ContentType, version = this.Version, defaults = this.Defaults }; }
/// <summary> /// ResizeImage图片地址生成 /// </summary> /// <param name="url">图片地址</param> /// <param name="w">最大宽度</param> /// <param name="h">最大高度</param> /// <param name="quality">质量0~100</param> /// <param name="image">占位图类别</param> /// <returns>地址为空返回null</returns> public static string ResizeImage(string url, int?w = null, int?h = null, int?quality = null, DummyImage?image = DummyImage.Default, ResizerMode?mode = null, ReszieScale?scale = null ) { var Url = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext); if (string.IsNullOrEmpty(url)) { return(null); } else { if (Url.IsLocalUrl(url)) { var t = new Uri(HttpContext.Current.Request.Url, Url.Content(url)).AbsoluteUri; Dictionary <string, string> p = new Dictionary <string, string>(); if (w.HasValue) { p.Add("w", w.ToString()); } if (h.HasValue) { p.Add("h", h.ToString()); } if (scale.HasValue) { p.Add("scale", scale.Value.ToString()); } if (quality.HasValue) { p.Add("quality", quality.ToString()); } if (image.HasValue) { p.Add("404", image.ToString()); } if (mode.HasValue) { p.Add("mode", mode.ToString()); } return(t + p.ToParam("?")); } else if (url.Contains(QinQiuApi.ServerLink)) { var fileType = System.IO.Path.GetExtension(url); StringBuilder sbUrl = new StringBuilder(url); if (fileType == ".mp4") { sbUrl.Append("?vframe/jpg/offset/1"); if (w.HasValue) { sbUrl.Append($"/w/{w}"); } if (h.HasValue) { sbUrl.Append($"/h/{h}"); } return(sbUrl.ToString()); } else { sbUrl.Append("?imageView2"); switch (mode) { case ResizerMode.Pad: default: case ResizerMode.Crop: sbUrl.Append("/1"); break; case ResizerMode.Max: sbUrl.Append("/0"); break; } if (w.HasValue) { sbUrl.Append($"/w/{w}"); } if (h.HasValue) { sbUrl.Append($"/h/{h}"); } quality = quality ?? 100; sbUrl.Append($"/q/{quality}"); return(sbUrl.ToString()); } } else { return(url); } } }
public void SendOrderReceived(Order order, HttpRequestBase Request, System.Web.Mvc.UrlHelper Url) { var fileContents = System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/Content/Emails/OrderReceived.html")); fileContents = fileContents.Replace("[#ORDERNO#]", StringHelper.GetOrderNo(order.OrderId, order.OrderDate)); fileContents = fileContents.Replace("[#ORDERDATE#]", order.OrderDate.ToShortDateString()); fileContents = fileContents.Replace("[#PAYMENT#]", order.Paid ? "PAID" : "NOT PAID"); fileContents = fileContents.Replace("[#TOTALPRICE#]", order.TotalPrice.ToString()); var orderDetails = order.GetOrderDetail(order.OrderId); string productsHtml = ""; foreach (var product in orderDetails.OrderProducts) { productsHtml += "<tr>"; productsHtml += "<td><img src =\"" + Request.Url.Scheme + "://" + Request.Url.Authority + Url.Content("~/ProductImage/" + product.FileName + "-1.jpg") + "\" /></td>"; productsHtml += "<td>" + product.VariantName + " " + product.ProductName + "</td>"; productsHtml += "<td>" + product.Quantity + "</td>"; productsHtml += "<td>" + product.UnitPrice + "</td>"; productsHtml += "<td>" + product.TotalPrice + "</td>"; productsHtml += "</tr>"; } fileContents = fileContents.Replace("[#PRODUCTS#]", productsHtml); var model = new EmailModel(); model.Body = fileContents; model.MailTo = order.BillingEmail; model.Title = "Order Received!"; string error = ""; SendEmail(model, ref error); }
/// <summary> /// 返回移动端的文件 /// 有域名,直接返回;没有域名的,经过Url.Content加上域名返回 /// </summary> /// <param name="urlHelper"></param> /// <param name="contentPath"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static string MobileAddr(this System.Web.Mvc.UrlHelper urlHelper, string contentPath, string defaultValue = "") { if (string.IsNullOrEmpty(contentPath)) { return(defaultValue); } if (contentPath.StartsWith("http://")) { return(contentPath); } string mainHostName = ConfigurationManager.AppSettings["mainSite"]; string sitePort = ConfigurationManager.AppSettings["sitePort"]; return("http://" + mainHostName + ((string.IsNullOrWhiteSpace(sitePort) || sitePort == "80") ? "" : (":" + sitePort)) + (contentPath.StartsWith("~") ? urlHelper.Content(contentPath) : contentPath)); }
private void AddHomeLink(StringBuilder sb) { sb.Append("<li>"); sb.Append("<a href=\"" + Url.Content("~") + "\" title=\"Home\">Home</a>"); sb.Append("</li>"); }