Search code examples
asp.neturlmultilingual

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


I am developing a website with multiple languages (Two) and it must have friendly URLs for optimize SEO. I'm using normal globalization (CurrentCulture + resource files in both languages). Now I have a Global.asax file with the following code:

<%@ 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>

And a file called DefaultRouteHandeler.vb inside the App_Code folder, it contains the following code:

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 

When I run the site the following error message appears:

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

Why it is showing this error?

I got those codes from this question: Add second language support with root path in an existing ASP.NET WebForms site


Solution

  • Try adding the Implements clause after the function.

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

    UPDATE: Give this a try

    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