Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the current executing controller from HttpContext?

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. }

like image 439
Jaggu Avatar asked Oct 07 '11 08:10

Jaggu


People also ask

What does HttpContext current do?

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.

Is HttpContext current items thread-safe?

HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.


2 Answers

You can search the values of "controller" and "action" in this object

HttpContext.Request.RequestContext.RouteData.Values
like image 109
Massimo Zerbini Avatar answered Sep 30 '22 02:09

Massimo Zerbini


Not sure in what context you are executing, but you can get it from the RequestContext:

RequestContext.RouteData.Values["controller"].ToString()
like image 45
Paddy Avatar answered Sep 30 '22 02:09

Paddy