VB.NET MVC 3 应用程序中的 Html 扩展
我使用 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
您所做的就是向
HtmlHelper
类添加两个扩展方法。当您使用这些方法时,ByVal helper as HtmlHelper
会作为HtmlHelper
类的实例隐式传递。这是在视图中使用它们的方式:
希望这会有所帮助。
What you've done is add two extension methods to the
HtmlHelper
class. TheByVal helper as HtmlHelper
is passed implicitly as an instance of theHtmlHelper
class when you use these methods.This is how you use them in a view:
Hope this helps.