Reputation: 5145
I'm confused about MVC routes. Let's say I have this domain: poopoopants.com. I want some standard URLs off the root that go to an "About" page or "Contact" page:
http://poopoopants.com/about
http://poopoopants.com/contact
Now, I'm also going to have an infinite amount of these two routes below, where "[user]" is a variable for the username for a person registered with an account on poopoopants.com, and [file-path] is a URL-sanitize file path that may contain the / character:
http://poopoopants.com/[user]
http://poopoopants.com/[user]/[file-path]
So I'm assuming I'd have one IndexController
with an Index
action for /
, About
action for /about
, Contact
action for /contact
, and one UserController
with Index
action for /[user]/
and File
action for /[user]/[file-path]
.
My question concerns definite the routes in Global.asax.cs
. So far I've got:
routes.MapRoute("Index", "/", new { controller = "Index", action = "Index" });
routes.MapRoute("About", "/about", new { controller = "Index", action = "About" });
routes.MapRoute("Contact", "/contact", new { controller = "Index", action = "Contact" });
But what do I specify for the /[user] and /[user]/[file-path] routes, and what are the corresponding method signatures of their actions in UserController
?
Upvotes: 1
Views: 681
Reputation: 1054
I didn't test them, but it should work:
Routes:
routes.MapRoute(
"User",
"{username}",
new { controller = "User", action = "Index" }
);
routes.MapRoute(
"File",
"{username}/{filepath}",
new { controller = "User", action = "File" }
Method signatures:
public ActionResult Index(string username);
public ActionResult File(string username, string filepath);
Also, you can make your Index controller Route probably in one MapRoute clause.
Usefull tool: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Upvotes: 2