Asp .Net MVC 获取请求路由数据

protected String RouteController
{
    get
    {
        return GetRouteData("controller");
    }
}

protected String RouteAction
{
    get
    {
        return GetRouteData("action");
    }
}

protected String RouteID
{
    get
    {
        return GetRouteData("id");
    }
}

protected String GetRouteData(String key)
{
    if (String.IsNullOrEmpty(key))
        return null;
    // Mvc 都是使用小写字母
    key = key.ToLower();

    IDictionary<string, object> routeValues = this.ControllerContext.RouteData.Values;
    if (!routeValues.ContainsKey(key))
        return null;
    return routeValues[key].ToString();
}
posted @ 2020-09-15 15:48  Evilloafer  阅读(29)  评论(0编辑  收藏