Reputation: 1542
im building a high traffic asp.net webforms site. I have componentized all the urls in a class. When looking up the current route of the request what is most efficient? Calling HttpContext.Current.Request.RequestContext.RouteData.Route or sending Page.RouteData.Route into the method that checks to see if we are at a certain route url. Example:
public static bool IsCurrentRoute(string routeName)
{
var route = HttpContext.Current.Request.RequestContext.RouteData.Route;
if (route == System.Web.Routing.RouteTable.Routes[routeName])
return true;
return false;
}
Upvotes: 0
Views: 2670
Reputation: 3523
Page.Routedata just wraps around Context.RouteData.
Taken from JustDecompile
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public RouteData RouteData
{
get
{
if (base.Context != null && base.Context.Request != null)
{
return base.Context.Request.RequestContext.RouteData;
}
return null;
}
}
So to answer your Question - no difference. Though you are less likely to get an exception if you use the Page method due to null checks. Though how often your context is going to be null is another question and is possibly a case you want to design for yourself if you think it has potential.
Also rather than accessing HttpContext.Current directly think about wrapping it in a HttpContextBase and using that.
An example can be found here http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate/ look at the line with
new HttpContextWrapper(HttpContext.Current)
Upvotes: 3