In searching I came across http://stackoverflow.com/questions/411054/asp-net-mvc-page-subpage-routing and the wildcard was somewhat of a lifesaver, but presented a new problem.
Consider the following routes, if you will. The goal here is to have the index (http://www.domain.com) use the default route and [domain]/page use the Content route. The strSlug is a wildcard because I plan on splitting the slug to use for subpages. [domain]page/subpage (subpage would be the current slug). With it being a wildcard, it seems like [domain] is matching on the Content route.
What would be the proper way around that?
// Global.asx.cs
routes.MapRoute(
"Content", // Route name
"{*strSlug}", // URL with parameters
new { controller = "Home", action = "SiteContent", strSlug = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
// HomeController.cs
// Not the best code, feel free to improve.
public ActionResult SiteContent(string strSlug)
{
string[] strSlug_arr = strSlug.Split("/".ToCharArray());
strSlug = strSlug_arr[(strSlug_arr.Length-1)];
SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
return View(data);
}
EDIT: In doing some more research, I was hoping to use something like this.
public ActionResult Index()
{
SiteContent data = DB.SiteContents.Single(c => c.Slug == "home-page");
return View(data);
}
public ActionResult SiteContent(string strSlug)
{
if (strSlug == null)
{
return RedirectToAction("Index");
}
else
{
string[] strSlug_arr = strSlug.Split("/".ToCharArray());
strSlug = strSlug_arr[(strSlug_arr.Length - 1)];
SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
return View(data);
}
}
..but this results in a redirect loop for some reason.
EDIT #2 So this solution kind of, sort of, maybe works? I have yet to see a downside.
// Global.asx.cs
routes.MapRoute(
"Content", // Route name
"{strSlug}/{*strSubSlug}", // URL with parameters
new { controller = "Home", action = "SiteContent", strSubSlug = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
// HomeController.cs
public ActionResult Index()
{
SiteContent data = DB.SiteContents.Single(c => c.Slug == "home-page");
return View(data);
}
public ActionResult SiteContent(string strSlug, string strSubSlug)
{
/*
string[] strSlug_arr = strSlug.Split("/".ToCharArray());
strSlug = strSlug_arr[(strSlug_arr.Length - 1)];
*/
if (strSubSlug != null)
{
string[] strSubSlug_arr = strSubSlug.Split("/".ToCharArray());
strSlug = strSubSlug_arr[(strSubSlug_arr.Length - 1)];
}
SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
return View(data);
}
So [domain]/slug works.. and anything after that it parses off the last slug.. so [domain]slug/sub/sub/sub/content would grab the page matching the content slug.