So, I need for my website to have this being passed around in the routing:
blah.com/sitename/{controller}/{action}/{id}
The "sitename" is like a Vdir, but not quite. It will help me get the right data etc... for the given sitename.
What is the best way of doing this? I tried doing this in the routing but no go as it cannot find the page (this is when I am trying direct to the login page):
routes.MapRoute(
name: "Default",
url: "{sitename}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Users will be given a url like blah.com/somesite or blah.com/anothersite.
I just want the ability for routing to work as normal but be able to extract the "somesite" or "anothersite" portion around the controllers.
First add a route to Global.aspx.cs
to pass a {sitename}
parameter:
routes.MapRoute(
"Sites", // Route name
"{sitename}/{controller}/{action}/{id}", // URL with parameters
new { sitename = "", controller = "Home", action = "Index", id = "" }// Parameter defaults
);
Create a New Controller called BaseController
. Then add the following simple code inside a base controller:
public class BaseController: Controller
{
public string SiteName = "";
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase req = filterContext.HttpContext.Request;
SiteName = filterContext.RouteData.Values["sitename"] as string;
base.OnActionExecuting(filterContext);
}
}
And use in your derived controller:
public class HomeController: BaseController
{
public ActionResult Index()
{
ViewData["SiteName"] = SiteName;
return View();
}
}
Hope this helps.