How to get RouteData.Value in class in ASP.NET webforms

178 views Asked by At

I am getting route data value in Page_Load but I want to get this value in a class.

string id = Page.RouteData.Values["id"]; // works in Page_Load

I have tried this code in a class but it didn't work. It's returning null. I don't want to check in Page_Load() on every page. I just want to check using a function in a class if it's possible.

RouteData routeData = HttpContext.Current.Request.RequestContext.RouteData;

if(routeData != null)
{
    var id = routeData.Values["id"];
}
else
{
    // Routedata returning null;
}

Any idea?

0

There are 0 answers