-1

Can some please help me convert the line number 1 into an APP_Code for ASP.NET 3.5

LINE 1.  RouteData RouteDatax = HttpContext.Current.Items["RouteData"] as System.Web.Routing.RouteData;

I have tried convert the above code like below but it didn't work.

public static class RouteDatax{
RouteData RouteDatax = HttpContext.Current.Items["RouteData"] as System.Web.Routing.RouteData;
 return RouteData;}

Thank you

dngo
  • 1

2 Answers2

0

For C# you don't need all those "as System.Web.Routing.RouteData". You declare your type with the "RouteData" at the beginning of the line. You may need to do some casting like this though:

RouteData RouteDatax = (RouteData)(HttpContext.Current.Items["RouteData"]);

That should make the line of code more valid. I'm not sure what you are meaning about converting the line into "an APP_Code" though...

Edit - Correcting other errors:

public static class RouteDatax
{
    public static string RouteDataxx = (RouteData)(HttpContext.Current.Items["RouteData"]);

}

The above code should compile. Changes made include:

  1. making the declared property static - can't have a non-static member of a static class
  2. removing the return statement - it makes no sense in a class context
  3. changing the name of the field - you can't have a member named the same as its enclosing type

This assumes you are trying to create a class with that as a field. You could also have made it a property like so:

    public RouteData myValue
    {
        get
        {
            RouteData RouteDatax = (RouteData)(HttpContext.Current.Items["RouteData"]);
            return RouteDatax;
        }
    }

or a method:

public static class RouteDatax
{
    public RouteData myValue()
    {
            RouteData RouteDatax = (RouteData)(HttpContext.Current.Items["RouteData"]);
            return RouteDatax;
    }
} 

Hope that helps.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • It still said at the return , "Invalid token in class,struck, or interface member declaration" public static class RouteDatax{ RouteData RouteData_x = (RouteData)(HttpContext.Current.Items["RouteData"]); return RouteData_x; } – dngo Aug 12 '10 at 16:44
  • @dngo: I still am not sure exactly what you are trying to do but hopefully the edit should get you where you want to be... – Chris Aug 13 '10 at 08:29
0

thank you so much for your helps. I really appreciate it. I am still having difficulty in calling the class out and use like RouteData.Values["routeid"]

What I like is to put the code below in the APP_CODE so I can call it from anywhere within the same application. For right now, every time I want to make a request I have to put the code below within the same page/code be hide to make a call. This is a bit too inconvenient and it will be hard to migrate from asp.net 3.5 to 4.0 framework.

RouteData RouteData = HttpContext.Current.Items["RouteData"] as System.Web.Routing.RouteData;

the only way to make it easy is to convert the above code so we can make a call from anywhere within the application(website)

Response.Write(RouteData.Values["routeid"]);
dngo
  • 1
  • 1