0

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

asked Apr 15, 2015 at 0:15

1 Answer 1

1

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
answered Apr 15, 2015 at 0:30
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thank you so much for your help, i did what you recommend, but now, i'm getting this error message: Compiler Error Message: BC32022: 'Public Event Load (sender As Object, e As System.EventArgs)' is an event and can not be called directly. Use the RaiseEvent statement to generate an event. Source Error: Line 21: Dim webform = TryCast (Page, Page) Line 22: If webform IsNot Nothing Then Line 23: webForm.Load + = Sub () webForm.Form.Action = requestContext.HttpContext.Request.RawUrl Line 24: End If Line 25: End If Source File: C:\mysite\App_Code\DefaultRouteHandeler.vb Line: 23
Q: Was this code originally C# code that was converted to VB.NET?
"Use RaiseEvent statement to generate an event" In VB.NET you use the RaiseEvent in the declaration. It's been a few years since I worked with web forms but I am going back to some old code now for reference for you.
Thanks, I'll be waiting for your help. Yes, it was originally C# code, but i converted that code with this: converter.telerik.com
Yes. I see. That is the issue. C# deals with events differently and the code needs modification after translation. I am working on something for you now.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.