Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Routedata.Values[""]?

I am surprised to see that there is no article which answers this question with any details. I have few questions related to RouteData.Values[""].

I saw this code:

public ActionResult Index()
{
    ViewBag.Message = string.Format("{0}---{1}--{2}",
        RouteData.Values["Controller"],
        RouteData.Values["action"],
        RouteData.Values["id"]);

    return View();
}

Here it is basically reading values which potentially sounds like "Meta-data" of the controller. Or is it something that View can also pass to Controller?

like image 957
Lost Avatar asked Mar 03 '15 02:03

Lost


People also ask

What is RouteValueDictionary?

The RouteValueDictionary class enables you to work with collections of key/value pairs that are required in order to use ASP.NET routing. You use the RouteValueDictionary object to define values for the Constraints, DataTokens, and Defaults properties.

What is router data in ASP NET MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController.

What is the use of Route attribute in MVC?

MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application. The earlier style of routing, called convention-based routing, is still fully supported.


1 Answers

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"]);
    }
    
like image 192
Pramod Mangalore Avatar answered Oct 05 '22 18:10

Pramod Mangalore