2014-02-17 51 views
2

我一直在努力研究如何將標籤擴展類註冊到ServiceStack.Html/Razor項目中。我正在使用「獨立,自託管的HttpListener」選項,但我無法確定如何註冊或使用新的@Html擴展程序以用於剃鬚刀頁面。ServiceStack.Html自定義標籤擴展

namespace Tribe.Guru.SelfHost 
{ 
    public static class LabelExtensions 
    { 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) 
     { 
      return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes)); 
     } 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) 
     { 
      ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); 
      string htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
      string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); 
      if (String.IsNullOrEmpty(labelText)) 
      { 
       return MvcHtmlString.Empty; 
      } 

      TagBuilder tag = new TagBuilder("label"); 
      tag.MergeAttributes(htmlAttributes); 
      tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); 
      tag.SetInnerText(labelText); 
      return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); 
     } 
    } 
} 

任何幫助將不勝感激,因爲我找不到任何文件,並不能得到它的工作。

回答

1

您還需要任何的命名空間添加到您的Web.config剃刀配置,e.g:

<system.web.webPages.razor> 
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
<pages pageBaseType="ServiceStack.Razor.ViewPage"> 
    <namespaces> 
    <add namespace="ServiceStack"/> 
    <add namespace="ServiceStack.Html"/> 
    <add namespace="ServiceStack.Razor"/> 
    <add namespace="ServiceStack.Text"/> 
    <add namespace="ServiceStack.OrmLite"/> 
    <add namespace="Tribe.Guru.SelfHost"/> 
    </namespaces> 
</pages> 
</system.web.webPages.razor>