VB.NET MVC 3 应用程序中的 Html 扩展

发布于 11-16 03:22 字数 3430 浏览 2 评论 0原文

我使用 VB.NET 实现了一个模块,以便处理 MVC .NET 应用程序中的本地化。

但是,当我尝试从我的角度调用扩展方法 LanguageSelectorLink 时,需要为 HtmlHelper 类型的第一个参数提供一些内容。在我在 Internet 上找到的所有 C# 示例中,从未提供此参数,但它似乎工作正常。

@App_Code.LanguageSelectorLink(Nothing, "en-CA", "[English]", "English", Nothing)

我错过了一个步骤吗?有人可以帮助我吗?

导入 System.Threading

命名空间 App_Code

Public Module SwitchLanguageHelper
    <System.Runtime.CompilerServices.Extension()>
    Public Function LanguageUrl(ByVal helper As HtmlHelper,
                                       ByVal cultureName As String,
                                       Optional ByVal languageRouteName As String = "lang",
                                       Optional ByVal strictSelected As Boolean = False) As Language
        'Set the input language to lower
        cultureName = cultureName.ToLower()

        'retrieve the route values from the view context
        Dim routeValues = New RouteValueDictionary(helper.ViewContext.RouteData.Values)

        'copy the query strings into the route values to generate the link
        Dim queryString = helper.ViewContext.HttpContext.Request.QueryString

        For Each key In queryString
            If (Not (IsNothing(queryString(key))) And
               Not (String.IsNullOrWhiteSpace(queryString(key)))) Then
                If (routeValues.ContainsKey(key)) Then
                    routeValues(key) = queryString(key)
                Else
                    routeValues.Add(key, queryString(key))
                End If

            End If
        Next

        Dim actionName = routeValues("action").ToString()
        Dim controllerName = routeValues("controller").ToString()

        'set the language into route values
        routeValues(languageRouteName) = cultureName

        'generate the language specify url
        Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection)
        Dim url = urlHelper.RouteUrl("Localization", routeValues)

        'check whether the current thread ui culture is this language
        Dim current_lang_name = Thread.CurrentThread.CurrentUICulture.Name.ToLower()
        Dim isSelected As Boolean

        If (strictSelected) Then
            current_lang_name = cultureName
        Else
            current_lang_name.StartsWith(cultureName)
        End If

        isSelected = current_lang_name

        Dim language As Language = New Language()

        language.Url = url
        language.ActionName = actionName
        language.ControllerName = controllerName
        language.RouteValues = routeValues
        language.IsSelected = isSelected

        Return language
    End Function

    <System.Runtime.CompilerServices.Extension()>
    Public Function LanguageSelectorLink(ByVal helper As HtmlHelper, ByVal cultureName As String, ByVal selectedText As String, ByVal unselectedText As String, ByVal htmlAttributes As IDictionary(Of String, Object), Optional ByVal languageRouteName As String = "lang", _
                                         Optional ByVal strictSelected As Boolean = False) As MvcHtmlString
        Dim language = helper.LanguageUrl(cultureName, languageRouteName, strictSelected)
        Dim link = helper.RouteLink(If(language.IsSelected, selectedText, unselectedText), "Localization", language.RouteValues, htmlAttributes)

        Return link
    End Function


End Module

End 命名空间

I implemented a Module using VB.NET in order to handle localization in an MVC .NET application.

However, when I try to call the extension method LanguageSelectorLink from my view, it`s required to provide something for the first parameter of type HtmlHelper. In all C# samples I found on the Internet, this parameter is never provided and it seems to work fine.

@App_Code.LanguageSelectorLink(Nothing, "en-CA", "[English]", "English", Nothing)

Did I miss a step? Can anybody help me?

Imports System.Threading

Namespace App_Code

Public Module SwitchLanguageHelper
    <System.Runtime.CompilerServices.Extension()>
    Public Function LanguageUrl(ByVal helper As HtmlHelper,
                                       ByVal cultureName As String,
                                       Optional ByVal languageRouteName As String = "lang",
                                       Optional ByVal strictSelected As Boolean = False) As Language
        'Set the input language to lower
        cultureName = cultureName.ToLower()

        'retrieve the route values from the view context
        Dim routeValues = New RouteValueDictionary(helper.ViewContext.RouteData.Values)

        'copy the query strings into the route values to generate the link
        Dim queryString = helper.ViewContext.HttpContext.Request.QueryString

        For Each key In queryString
            If (Not (IsNothing(queryString(key))) And
               Not (String.IsNullOrWhiteSpace(queryString(key)))) Then
                If (routeValues.ContainsKey(key)) Then
                    routeValues(key) = queryString(key)
                Else
                    routeValues.Add(key, queryString(key))
                End If

            End If
        Next

        Dim actionName = routeValues("action").ToString()
        Dim controllerName = routeValues("controller").ToString()

        'set the language into route values
        routeValues(languageRouteName) = cultureName

        'generate the language specify url
        Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection)
        Dim url = urlHelper.RouteUrl("Localization", routeValues)

        'check whether the current thread ui culture is this language
        Dim current_lang_name = Thread.CurrentThread.CurrentUICulture.Name.ToLower()
        Dim isSelected As Boolean

        If (strictSelected) Then
            current_lang_name = cultureName
        Else
            current_lang_name.StartsWith(cultureName)
        End If

        isSelected = current_lang_name

        Dim language As Language = New Language()

        language.Url = url
        language.ActionName = actionName
        language.ControllerName = controllerName
        language.RouteValues = routeValues
        language.IsSelected = isSelected

        Return language
    End Function

    <System.Runtime.CompilerServices.Extension()>
    Public Function LanguageSelectorLink(ByVal helper As HtmlHelper, ByVal cultureName As String, ByVal selectedText As String, ByVal unselectedText As String, ByVal htmlAttributes As IDictionary(Of String, Object), Optional ByVal languageRouteName As String = "lang", _
                                         Optional ByVal strictSelected As Boolean = False) As MvcHtmlString
        Dim language = helper.LanguageUrl(cultureName, languageRouteName, strictSelected)
        Dim link = helper.RouteLink(If(language.IsSelected, selectedText, unselectedText), "Localization", language.RouteValues, htmlAttributes)

        Return link
    End Function


End Module

End Namespace

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮2024-11-23 03:22:57

您所做的就是向 HtmlHelper 类添加两个扩展方法。当您使用这些方法时,ByVal helper as HtmlHelper 会作为 HtmlHelper 类的实例隐式传递。

这是在视图中使用它们的方式:

@Imports YourApplicationName.AppCode

@Html.LanguageSelectorLink("en-CA", "[English]", "English", Nothing)

希望这会有所帮助。

What you've done is add two extension methods to the HtmlHelper class. The ByVal helper as HtmlHelper is passed implicitly as an instance of the HtmlHelper class when you use these methods.

This is how you use them in a view:

@Imports YourApplicationName.AppCode

@Html.LanguageSelectorLink("en-CA", "[English]", "English", Nothing)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文