Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing ViewContext from class library

Is it possible to access ViewContext from class library? I am in need of getting names of current View and Controller's action. I added both System.Web.dll and System.Web.Mvc in my class library project but still unable to find a way to get what I require. Although I can access current context using System.Web.HttpContext.Current.

like image 736
Waqas Avatar asked Dec 30 '10 15:12

Waqas


1 Answers

You can access current route information like this:

var httpContext = new HttpContextWrapper(HttpContext.Current);
var routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(httpContext);

var controllerName = routeData.Values["controller"].ToString();
var actionName = routeData.Values["action"].ToString();
like image 103
TheCloudlessSky Avatar answered Sep 22 '22 19:09

TheCloudlessSky