I\'m trying to fetch the current page from my shared layout in .net mvc app so that I can load a different favicon icon for the 2 different pages.
I\'ve tried something
You can get your current controller name & action method name from your RouteData dictionary.
@{
var controllerName = string.Empty;
object controllerObj;
var actionName = string.Empty;
object actionObj;
if (ViewContext.RouteData.Values.TryGetValue("controller", out controllerObj))
{
controllerName = controllerObj.ToString();
}
if (ViewContext.RouteData.Values.TryGetValue("action", out actionObj))
{
actionName = actionObj.ToString();
}
}
I use something this to change my layout for some particular reasons with users that are not logged in or hasn't got claims yet.
Take consideration, Values will are keys where:
Value[0] = controller
Value[1] = action
using this logic:
if (ViewContext.RouteData.Values.Where(v => v.Key == "action").FirstOrDefault().Value.ToString() == "MyAction")
{
Layout = "_Layout_MyOtherLayout";
}
else
{
Layout = "_Layout";
}