Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.net核心mvc 3.1中的HtmlHelper扩展方法中使用DataAnnotation定位器_C#_Localization_Asp.net Core Mvc_Data Annotations_Asp.net Core 3.1 - Fatal编程技术网

C# 在ASP.net核心mvc 3.1中的HtmlHelper扩展方法中使用DataAnnotation定位器

C# 在ASP.net核心mvc 3.1中的HtmlHelper扩展方法中使用DataAnnotation定位器,c#,localization,asp.net-core-mvc,data-annotations,asp.net-core-3.1,C#,Localization,Asp.net Core Mvc,Data Annotations,Asp.net Core 3.1,我希望将扩展方法设置为HTML Helper,以显示ViewModel属性的描述。这里是清单,因为ASP.NET Core 3.1中的情况发生了变化 以下是我的扩展方法: public static string DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression) { Member

我希望将扩展方法设置为HTML Helper,以显示ViewModel属性的描述。这里是清单,因为ASP.NET Core 3.1中的情况发生了变化

以下是我的扩展方法:

public static string DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression) {
    MemberExpression memberExpression = (MemberExpression)expression.Body;
    var displayAttribute = (DisplayAttribute)memberExpression.Member.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
    string description = displayAttribute?.Description ?? memberExpression.Member?.Name;
    //But how can i localize this description
    return description;
}

如何在扩展方法中检索DataAnnotationLocalizer?当然,我可以像参数一样传递它,但是当
DisplayNameFor
不要求附加参数时,这不是一个很好的解决方案。

您只需要获得对
IStringLocalizer
的引用

在您的启动中:

public void Configure(..., IStringLocalizer stringLocalizer) // ASP.NET Core will inject it for you
{
    // your current code
    YourExtensionsClass.RegisterLocalizer(stringLocalizer);
}
在你的扩展课程中:

public static class YourExtensionsClass
{
    private static IStringLocalizer _localizer;

    public static void RegisterLocalizer(IStringLocalizer localizer)
    {
        _localizer = localizer;
    }

    public static string DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression) 
    {
        MemberExpression memberExpression = (MemberExpression)expression.Body;
        var displayAttribute = (DisplayAttribute)memberExpression.Member.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
        string description = displayAttribute?.Description ?? memberExpression.Member?.Name;
        
        return _localizer[description];
    }
}
公共静态类yourExtensionClass
{
专用静态IStringLocalizer\u定位器;
公共静态无效注册表定位器(IStringLocalizer定位器)
{
_定位器=定位器;
}
公共静态字符串描述(此IHtmlHelper自我表达式)
{
MemberExpression MemberExpression=(MemberExpression)expression.Body;
var displayAttribute=(displayAttribute)memberExpression.Member.GetCustomAttributes(typeof(displayAttribute),false).FirstOrDefault();
字符串描述=displayAttribute?.description??memberExpression.Member?.Name;
返回_定位器[说明];
}
}

如果您想获得更多的控制权,我建议您从ASP.NET Core的内部工作中获得一些想法。

谢谢!我有自己的IStringLocalizerFactory将类型转换为合适的资源位置,因为资源文件位置的默认约定对我的项目来说不是很合适。所以我改变了方法:description=\u LocalizerFactory.Create(memberExpression.Expression.Type)[description];
public static class YourExtensionsClass
{
    private static IStringLocalizer _localizer;

    public static void RegisterLocalizer(IStringLocalizer localizer)
    {
        _localizer = localizer;
    }

    public static string DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression) 
    {
        MemberExpression memberExpression = (MemberExpression)expression.Body;
        var displayAttribute = (DisplayAttribute)memberExpression.Member.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
        string description = displayAttribute?.Description ?? memberExpression.Member?.Name;
        
        return _localizer[description];
    }
}