简体   繁体   中英

Routes with multiple arguments in ASP.NET MVC 3

I am trying to set up dynamic routes in an MVC app, and I have this so far...

string conString = "YATAYATAYATA";

        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();

        // Retrieve routes from database
        cmd.CommandText = "SELECT R.*,S.* FROM Routes R INNER JOIN Sites S ON S.ID = R.SiteID WHERE S.ID = 1";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        con.Open();

        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            routes.MapRoute(
            rdr["name"].ToString(),             // Route name
            rdr["url"].ToString(),   // URL with parameters
                new
                {
                    controller = rdr["controller"].ToString(),  // Controller name
                    action = rdr["action"].ToString(),          // Action name
                    id = UrlParameter.Optional                 // Parameter defaults
                } 
            );
        }

And that is working great for now, the only issue I am having is that I would like to have the ability to specify a comma delimited list of optional arguments in the database that I could pull out like...

Array optParams = rdr["parametersOpt"].ToString().Split(',');

But I was not sure how to stick those params into the route object properly. Could just be a minor C# syntax I am not familiar with.

I very well may be wrong, but I believe you can use IDictionary<string, object> for the 3rd parameter to MapRoute instead of an anonymous object (MSDN documentation is unusually sparse for MVC).

I tried to whip up a test proj quick and it seems to work.

Here's what your code would look like if you tried to use a dictionary (again, I'm not certain this will work):

        while (rdr.Read()) {
            var defaults = new Dictionary<string, object>() {
                                                                {"controller", rdr["controller"].ToString()},
                                                                {"action", rdr["action"].ToString()},
                                                                {"id", UrlParameter.Optional}
                                                            };
            foreach (var param in rdr["parametersOpt"].ToString().Split(',')) {
                defaults.Add(param, UrlParameter.Optional);
            }

            routes.MapRoute(
                rdr["name"].ToString(),             // Route name
                rdr["url"].ToString(),   // URL with parameters
                defaults
            );
        }

Thanks to @qstarin I was pointed in the right thought process...

After much trouble and hardship...

while (rdr.Read())
        {
            Route invRoute = new Route(rdr["url"].ToString(), new MvcRouteHandler());

            RouteValueDictionary defaults = new RouteValueDictionary();
            defaults.Add("controller", rdr["controller"].ToString());
            defaults.Add("action", rdr["action"].ToString());

            Array arrParams = rdr["parametersOpt"].ToString().Split(',');

            foreach (string i in arrParams)
            {
                defaults.Add(i, UrlParameter.Optional);
            }

            invRoute.Defaults = defaults;

            routes.Add(rdr["name"].ToString(), invRoute);
        }

This was my solution...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM