Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 1.95 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Custom html helpers dont work in MVC3
  2. namespace MyNamespace.Web.Helpers
  3. {
  4.  
  5.     public static class HtmlExtensions
  6.     {
  7.         public static string RenderHeaderImages(this HtmlHelper html)
  8.         {
  9.             StringBuilder bldr = new StringBuilder();
  10.             List<string> files = (List<string>)HttpContext.Current.Application["HeaderImages"];
  11.  
  12.             bldr.AppendLine("<table><tr>");
  13.             foreach (string file in files)
  14.             {
  15.                 bldr.AppendLine(string.Format("<td><img class="headerImage" src="{0}/Content/Images/Header/{1}", alt="{2}" /></td>"
  16.                     , HtmlExtensions.GetAppPath(""),  file, file));
  17.             }
  18.             bldr.AppendLine("</table></tr>");
  19.  
  20.             return bldr.ToString();
  21.         }
  22.  
  23.         public static string GetAppPath(this HtmlHelper html)
  24.         {
  25.             return HtmlExtensions.GetAppPath("");
  26.         }
  27.  
  28.         public static string GetAppPath(this HtmlHelper html, string arg)
  29.         {
  30.             return HtmlExtensions.GetAppPath(arg);
  31.         }
  32.  
  33.         public static string GetAppPath(string arg)
  34.         {
  35.             if (HttpContext.Current.Request.ApplicationPath.ToString() == @"/")
  36.                 return "http://localhost:50194" + arg;
  37.             else
  38.                 return HttpContext.Current.Request.ApplicationPath.ToString() + arg;
  39.         }
  40.     }
  41. }
  42.        
  43. @using MyNamespace.Web.Helpers
  44.  
  45. <system.web.webPages.razor>
  46. <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  47. <pages pageBaseType="System.Web.Mvc.WebViewPage">
  48.   <namespaces>
  49.     <add namespace="System.Web.Mvc" />
  50.     <add namespace="System.Web.Mvc.Ajax" />
  51.     <add namespace="System.Web.Mvc.Html" />
  52.     <add namespace="System.Web.Routing" />
  53.     <add namespace="MyNamespace.Web.Helpers"/>
  54.   </namespaces>
  55. </pages>
  56. </system.web.webPages.razor>
  57.        
  58. @using MyProject.HtmlHelpers;
  59.        
  60. using System.Web.WebPages.Html;
  61.        
  62. using System.Web.Mvc