2

ラベル拡張クラスを ServiceStack.Html/Razor プロジェクトに登録する方法を考え出そうとしています。「スタンドアロンの自己ホスト型 HttpListener」オプション@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));
        }
    }
}

ドキュメントが見つからず、動作させることができないため、どんな助けでも大歓迎です。

4

1 に答える 1

1

また、Web.config カミソリ構成に名前空間を追加する必要もあります。

<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>
于 2014-02-17T15:11:21.917 に答える