mvc action controller area

时间:2022-12-15 08:55:55

获取控制器名称:

ViewContext.RouteData.Values["controller"].ToString();

获取Action名称:

ViewContext.RouteData.Values["action"].ToString();

获取路由参数值:

ViewContext.RouteData.Values[名称].ToString();

如:ViewContext.RouteData.Values["ID"].ToString(); 获取ID的值

获取area名称

在代码中:

ControllerContext.RouteData.DataTokens["area"]

在View中:

ViewContext.RouteData.DataTokens["area"]

具体代码

var action = ViewContext.RouteData.Values["Action"].ToString().ToLower();
var controllerName = ViewContext.RouteData.Values["controller"].ToString().ToLower();
var areaName = ViewContext.RouteData.DataTokens["area"].ToString().ToLower();


 var rd = httpContext.Request.RequestContext.RouteData;
string currentAction = rd.GetRequiredString("action");
string currentController = rd.GetRequiredString("controller");
string currentArea = rd.Values["area"] as string;