视图中访问 路由参数

1.获取url中controller和action

第一种:

   获取controller名称:ViewContext.RouteData.Values["controller"]

   获取action名称:ViewContext.RouteData.Values["action"]

第二种:

   ViewContext.Controller.ValueProvider.GetValue("controller").RawValue

   ViewContext.Controller.ValueProvider.GetValue("action").RawValue

 

 

2.遍历路由参数集合

@{
    //路由参数 至少得的两个参数 为 
    //控制器名称和Action名称

    //controller-----RotateDataOne
    //action-----RotateOne

    RouteValueDictionary rotate = this.ViewContext.RouteData.Values;
    
    <p>遍历输出所有的键值:</p>
    
    foreach (var key in rotate.Keys)
    {
    <div>@key-----@rotate[key]</div>
    }
    
    
    <h3>如有记录的参数总数:@this.ViewContext.RouteData.Values.Count
    </h3>
}

 

posted @ 2014-09-03 11:39  天马3798  阅读(172)  评论(0编辑  收藏  举报