不使用 MVC 的多语言 ASP.NET 网站(具有友好的 URL)

Multi language ASP.NET website (with friendly URLs) without using MVC

我正在开发一个具有多种语言(两种)的网站,它必须具有友好的 URL 以优化 SEO。我正在使用正常的全球化(两种语言的 CurrentCulture + 资源文件)。现在我有一个包含以下代码的 Global.asax 文件:

<%@ Application Language="VB" %>
<%@ Import Namespace="SampleWeb" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">

Private Sub RegisterRoutes(routes As RouteCollection)
    routes.Ignore("{resource}.asxd/{*pathInfo}")
    'Route Path
    'Default Route Values
    'constraint to say the locale must be 2 letters. You could also use 
something like "en-us|en-gn|ru" to specify a full list of languages
    'Instance of a class to handle the routing
    routes.Add(New Route("{locale}/{*url}", Nothing, New 
RouteValueDictionary() From { _
        {"locale", "[a-z]{2}"} _
    }, New Utility.Handlers.DefaultRouteHandeler()))

End Sub


Private Sub Application_Start(sender As Object, e As EventArgs)
    ' Code that runs on application startup
    RegisterRoutes(RouteTable.Routes)

End Sub


</script>

App_Code 文件夹中有一个名为 DefaultRouteHandeler.vb 的文件,它包含以下代码:

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Compilation
Imports System.Web.Routing
Imports System.Web.UI

Namespace Utility.Handlers
Public Class DefaultRouteHandeler
    Implements IRouteHandler
    Public Function GetHttpHandler(requestContext As RequestContext) As 
IHttpHandler
        'Url mapping however you want here: 

        Dim routeURL As String =    
TryCast(requestContext.RouteData.Values("url"), String)

        Dim pageUrl As String = "~/" + (If(Not    
[String].IsNullOrEmpty(routeURL), routeURL, ""))

        Dim page = 
TryCast(BuildManager.CreateInstanceFromVirtualPath(pageUrl, 
GetType(Page)), IHttpHandler)
        If page IsNot Nothing Then
            'Set the <form>'s postback url to the route 
            Dim webForm = TryCast(page, Page)
            If webForm IsNot Nothing Then
                webForm.Load += Sub() webForm.Form.Action = 
requestContext.HttpContext.Request.RawUrl
            End If
        End If
        Return page
    End Function
End Class
End Namespace 

当我 运行 站点出现以下错误消息时:

Server Error in '/' Application. Compile error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code accordingly.

Compiler Error Message: BC30149: Class 'DefaultRouteHandeler' must implement 'Function GetHttpHandler (RequestContext As RequestContext) As IHttpHandler' for 'System.Web.Routing.IRouteHandler' interface.

Source Error:

Línea 9: Namespace Utility.Handlers Línea 10: Public Class DefaultRouteHandeler Línea 11: Implements IRouteHandler Línea 12: Public Function GetHttpHandler(requestContext As
RequestContext) As IHttpHandler Línea 13: 'Url mapping however you want here:

Source File: C:\Mysite\App_Code\DefaultRouteHandeler.vb Line 11

为什么会显示这个错误?

我从这个问题中得到了这些代码:Add second language support with root path in an existing ASP.NET WebForms site

尝试在函数后添加 Implements 子句。

Public Function GetHttpHandler(requestContext As RequestContext) As 
IHttpHandler Implements IRouteHandler.GetHttpHandler

更新:试试这个

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Compilation
Imports System.Web.Routing
Imports System.Web.UI

Namespace SampleWeb.Utility.Handlers
    Public Class DefaultRouteHandler
        Implements IRouteHandler

        Public Function GetHttpHandler(requestContext As RequestContext) As IHttpHandler Implements IRouteHandler.GetHttpHandler
            'Url mapping however you want here: 

            Dim routeURL As String = TryCast(requestContext.RouteData.Values("url"), String)

            Dim pageUrl As String = "~/" + (If(Not [String].IsNullOrEmpty(routeURL), routeURL, ""))

            Dim page = TryCast(BuildManager.CreateInstanceFromVirtualPath(pageUrl, GetType(Page)), IHttpHandler)
            If page IsNot Nothing Then
                'Set the <form>'s postback url to the route 
                Dim webForm As Page = TryCast(page, Page)
                If webForm IsNot Nothing Then
                    AddHandler webForm.Load, AddressOf webForm_Load
                End If
            End If
            Return page
        End Function

        Protected Sub webForm_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        End Sub
    End Class


End Namespace