我在C#中找到了4或5个“小写路由”的例子但是当我使用telerik代码转换器时,我收到了一堆错误。
即与"extension methods can be defined only in modules."
有没有人有关于如何在VB中将路由映射为小写的任何好资源?
这是一个错误的转换代码的示例
Imports System
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace MyMvcApplication.App.Helpers
Public Class LowercaseRoute
Inherits System.Web.Routing.Route
Public Sub New(ByVal url As String, ByVal routeHandler As IRouteHandler)
MyBase.New(url, routeHandler)
End Sub
Public Sub New(ByVal url As String, ByVal defaults As RouteValueDictionary, ByVal routeHandler As IRouteHandler)
MyBase.New(url, defaults, routeHandler)
End Sub
Public Sub New(ByVal url As String, ByVal defaults As RouteValueDictionary, ByVal constraints As RouteValueDictionary, ByVal routeHandler As IRouteHandler)
MyBase.New(url, defaults, constraints, routeHandler)
End Sub
Public Sub New(ByVal url As String, ByVal defaults As RouteValueDictionary, ByVal constraints As RouteValueDictionary, ByVal dataTokens As RouteValueDictionary, ByVal routeHandler As IRouteHandler)
MyBase.New(url, defaults, constraints, dataTokens, routeHandler)
End Sub
Public Overrides Function GetVirtualPath(ByVal requestContext As RequestContext, ByVal values As RouteValueDictionary) As VirtualPathData
Dim path As VirtualPathData = MyBase.GetVirtualPath(requestContext, values)
If path IsNot Nothing Then
path.VirtualPath = path.VirtualPath.ToLowerInvariant()
End If
Return path
End Function
End Class
Public NotInheritable Class RouteCollectionExtensions
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Sub MapRouteLowercase(ByVal routes As RouteCollection, ByVal name As String, ByVal url As String, ByVal defaults As Object)
routes.MapRouteLowercase(name, url, defaults, Nothing)
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Sub MapRouteLowercase(ByVal routes As RouteCollection, ByVal name As String, ByVal url As String, ByVal defaults As Object, ByVal constraints As Object)
If routes Is Nothing Then
Throw New ArgumentNullException("routes")
End If
If url Is Nothing Then
Throw New ArgumentNullException("url")
End If
Dim route = New LowercaseRoute(url, New MvcRouteHandler()) With { _
.Defaults = New RouteValueDictionary(defaults), _
.Constraints = New RouteValueDictionary(constraints) _
}
If [String].IsNullOrEmpty(name) Then
routes.Add(route)
Else
routes.Add(name, route)
End If
End Sub
End Class
End Namespace
答案 0 :(得分:1)
要解决您的“扩展方法只能在模块中定义”。问题: 更改“NotInheritable class”,您将这些方法定义为“Module”并删除构造函数。