I am using some 3rd party classes. I want to get the routevalues of my controller in that. Unfortunately it doesn't hand me the current controller that is executing. Can I get it from HttpContext?
The class looks something like:
public class ServiceStationVisibilityProvider
: ISiteMapNodeVisibilityProvider
{
public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
{
node.Title = DateTime.Now.ToString(); //need to access routevalues and set title
return true;
}
Now I could manully inspect Request.RawUrl and parse and do funky things. However, I don't want to write that kind and fall into trouble later when the application grows. }
The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.
HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.
You can search the values of "controller"
and "action"
in this object
HttpContext.Request.RequestContext.RouteData.Values
Not sure in what context you are executing, but you can get it from the RequestContext:
RequestContext.RouteData.Values["controller"].ToString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With