2

I tried to use ASP.Net's in the following generic handler, but i get this error at Page.RouteData

Reference to a non-shared member requires an object reference

What i am doing wrong?

<%@ WebHandler Language="VB" Class="MainHandler" %>

Imports System
Imports System.Web
Imports System.Xml


    Public Class MainHandler : Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState

        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

            Dim lng As String = Page.RouteData.Values("locale")

        End Sub

        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

    End Class

For any answers, please keep in mind that i am a newbie, and my tongue language is VB.NET :) Thank you in advance.

OrElse
  • 9,709
  • 39
  • 140
  • 253

2 Answers2

2
 Dim lng As String = HttpContext.Current.Request.RequestContext.RouteData.Values("locale")
OrElse
  • 9,709
  • 39
  • 140
  • 253
  • You are permitted to accept your own answer. The answer could be improved by explaining how and why it fixed your problem. It may even garner a few up votes. :) – Herbert Sep 10 '12 at 18:01
0

Try the following.You have to retrieve the route table and read from it.

Dim rd AS RouteData
rd = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context))

Dim val AS String
val = rd.Values["locale"].ToString()

Take a look at this link and msdn for more details on routing.

coder net
  • 3,447
  • 5
  • 31
  • 40
  • @net i think i have an extra issue here... Type 'MainHandler' does not inherit from 'System.Web.UI.Page'. – OrElse Aug 03 '11 at 21:36
  • I think you are implementing this wrong. Can you take a look at the links I put up. – coder net Aug 03 '11 at 21:39
  • @net I am sure i am implementing this wrong, since it is not working :( Actually i came here after reading the second link. I appreciate your help – OrElse Aug 03 '11 at 21:52
  • This is a simpler link. http://chriscavanagh.wordpress.com/2008/03/11/aspnet-routing-goodbye-url-rewriting/ . – coder net Aug 03 '11 at 22:28
  • I have a feeling that i am toooo newbie! I do not have problem implementing the ASP.NET Routing in .aspx pages. I have problem implementing the asp routing within a generic handler. So i can use that "page" thing, into the generic handler... – OrElse Aug 05 '11 at 09:06
  • ok, its tough to explain the whole thing here. but if you are just looking to the value, you can do context.Request.RequestContext.RouteData.Values["locale"]) instead of using page.RouteData in the processrequest function. checkout this link for more details. http://petesdotnet.blogspot.com/2009/09/generic-handlers-and-aspnet-routing.html . – coder net Aug 05 '11 at 13:52
  • And that is getting me to the post above... VB.NET & newbie :) – OrElse Aug 06 '11 at 12:10
  • From the http://petesdotnet.blogspot.com/2009/09/generic-handlers-and-aspnet-routing.html link, it looks like you need to implement an `IRouteHandler` that will create an instance of your `IHttpHandler`, and register that with the route. (You might have done this already? If so, please show...) Then, in that route handler you need to grab the `RequestContext` in your implementation of `GetHttpHandler` and store it in an instance variable of your `IHttpHandler`. *Then* you can use the `RequestContext` in your `IHttpHandler`. – GalacticCowboy Aug 06 '11 at 12:33
  • The above stated a slightly different way - can you show how you're registering the route? – GalacticCowboy Aug 06 '11 at 12:39