What is Routedata.Values[“”]?

假装没事ソ 提交于 2019-12-03 10:19:06

RouteData.Values is used for accessing the values/querystring values inserted by the classes handling routing.
In your case, the route defined in your route configuration class has additional parameters to which arguments would have been provided.
The parameters are controller, action, id.
The arguments to these parameters would have been provided somewhere in your code.

It makes more sense when you start a few levels higher, so you know what you are searching for.

  1. The Global.asax.cs

    protected void Application_Start(object sender, EventArgs e)
    {
        routingActions.RegisterCustomRoutes(RouteTable.Routes);
    }
    
  2. Another class defines the above method:

    public void RegisterCustomRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("searchdetails", "searchdetails/{orderID}/{PageIndex}/{PageSize}", "~/View/SearchDetails.aspx");
    }
    
  3. The following code creates a hyperlink. The main difference is the way the HREF is constructed. In this case, the "searchdetails" is defined in the class which contains my route configuration.

    linkToDetails.HRef = GetRouteUrl("searchdetails",
                    new
                    {
                        orderID = someOrderID,
                        PageIndex = currentPageIndex,
                        PageSize = PageSize
                    });
    
  4. Finally, the target page needs to use this information passed in step 3. This is where we use RouteData.Values[""]

    protected void Page_Load(object sender, EventArgs e)
    {
        var _orderid = Page.RouteData.Values["orderID"].ToString();
        var _PageIndex = Convert.ToInt32(Page.RouteData.Values["PageIndex"]);
        var _PageSize = Convert.ToInt32(Page.RouteData.Values["PageSize"]);
    }
    

RouteData is an attribute of the basic Controller class, so you can access RouteData in any controller. RouteData contains routing information for the current request. You can use RouteData to get controller, operation or parameter information as shown below.

Note that you need to convert to the appropriate data type or use the implicit type variable var.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!