Reputation: 4404
I was wondering how to setup routing in my MVC4 application where I can have a controller named TMZ and have it handle all of these routes:
/TMZ/About
/TMZ/Webinars
/TMZ/News
/TMZ/Conferment
/TMZ/CustomerCare
/TMZ/Marketing/Emails
/TMZ/Marketing/Brochures
/TMZ/Marketing/Print
/TMZ/Marketing/Press
/TMZ/Marketing/Presentations
/TMZ/Marketing/Graphics
/TMZ/Marketing/OCSRY
/TMZ/Marketing/Resources
/TMZ/Marketing/DesignStandards
/TMZ/Marketing/Videos
/TMZ/Marketing/PromoKits
/TMZ/Faculty/Forms
/TMZ/Faculty/Reports
/TMZ/CE/Guides
/TMZ/CE/Reports
/TMZ/Academy/Papers
/TMZ/Academy/Books
/TMZ/Academy/Promotions
/TMZ/ManualOfOperations
Showing Code:
Here is my RouteConfig.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace LicenseeArchive
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("TMZ",
"TMZ/{action}/{subaction}/{id}",
new { controller = "TMZ", action = "Index", subaction = UrlParameter.Optional, id = UrlParameter.Optional },
null,
new[] { "LicenseeArchive.Web.Controllers" });
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
null,
new[] { "LicenseeArchive.Web.Controllers" });
}
}
}
Here is my Global.asax.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace LicenseeArchive
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
}
Here is my TMZController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LicenseeArchive.Controllers
{
public class TMZController : Controller
{
//
// GET: /TMZ/
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Marketing(string subaction)
{
string _view = "Index";
switch (subaction)
{
case "Brochures":
_view = "Marketing/Brochures";
break;
}
return View(_view);
}
}
}
And here is my View folder structure:
Views
Account
Login.cshtml
Manage.cshtml
Register.cshtml
Home
About.cshtml
Contact.cshtml
Index.cshtml
Shared
_Footer.cshtml
_Head.cshtml
_HeaderBlock.cshtml
_Layout.cshtml
_LeftAside.cshtml
_LoginPartial.cshtml
_TopNav.cshtml
Error.cshtml
TMZ
Academy
Books.cshtml
Papers.cshtml
Promotions.cshtml
CE
Guides.cshtml
Reports.cshtml
Faculty
Forms.cshtml
Reports.cshtml
Marketing
Emails.cshtml
Brochures.cshtml
Print.cshtml
Press.cshtml
Presentations.cshtml
Graphics.cshtml
OCSRY.cshtml
Resources.cshtml
DesignStandards.cshtml
Videos.cshtml
PromoKits.cshtml
About.cshtml
Conferment.cshtml
CustomerCare.cshtml
News.cshtml
ManualOfOperations.cshtml
Webinars.cshtml
_ViewStart.cshtml
Web.Config
Upvotes: 0
Views: 195
Reputation: 2336
The simplest way would be modify the "default" route in Global.asax.cs
routes.MapRoute("Default",
"{controller}/{action}/{subaction}/{id}",
new {subaction= UrlParameter.Optional, id = UrlParameter.Optional});
This would mean you could use the same url format in other Controllers as well. Your TMZController would look something like:
public class TMZController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Marketing(string subaction)
{
return View();
}
}
Because the subaction/id are both optional you can construct 2, 3 or 4-part urls and just pickup the extra parameters in your actions, then handle them as required within the body of the methods.
Upvotes: 2
Reputation: 14074
routes.MapRoute("TMZ",
"TMZ/{action}/{id}",
new {controller = "TMZ", action = "Index", id = UrlParameter.Optional},
null,
new[] {"YourNamespace.Web.Controllers"});
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional},
null,
new[] {"YourNamespace.Web.Controllers"});
In this case i assumed that you've a Controller named TMZ and in that controller you've set all other actions including Marketing, Academy, Faculty, CE
But here is two things important to consider
TMZController
should have an string id parameter to handle the request. this id would be passed something like "Resources" for the Marketing action or "Books" for Academy Action.Hope this was clear.
Upvotes: 1