0

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.

2
  • 2
    You should accept answers to your questions. Commented Jan 19, 2011 at 16:28
  • Oops...I guess I forgot to do that...Thanks! I am clearly a noob... Commented Jan 19, 2011 at 16:30

2 Answers 2

1

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
            );
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Would you mind a quick sample of what you used? That would be great!
edited my answer. I'm still not totally certain a dictionary can be used in place of the anonymous object. If it can, this should work .. if I understand what you're doing with the parameters correctly.
I had one typo - mistakenly quoted the param variable. I've fixed it in my answer.
1

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.