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

时间:2015-04-15 00:15:55

标签: asp.net url multilingual

我正在开发一个多种语言的网站(两个),它必须有友好的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 

运行网站时,会显示以下错误消息:

'/'应用程序中的服务器错误。 编译错误 描述:编译资源期间发生错误
需要为此请求提供服务。请查看以下具体内容 错误详细信息并相应地修改源代码。

编译器错误消息:BC30149:类'DefaultRouteHandeler'必须 实现'Function GetHttpHandler(RequestContext As RequestContext)As IHttpHandler'用于'System.Web.Routing.IRouteHandler'界面。

来源错误:

Línea9:Namespace Utility.Handlers Línea10:公共类DefaultRouteHandeler Línea11:实施IRouteHandler Línea12:公共函数GetHttpHandler(requestContext as
RequestContext)作为IHttpHandler Línea13:'你想要的网址映射:

源文件:C:\ Mysite \ App_Code \ DefaultRouteHandeler.vb第11行

为什么会显示此错误?

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

1 个答案:

答案 0 :(得分:1)

尝试在函数后添加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