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();
}
相關文章
相關標籤/搜索