0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. yes, I found this/pretty much did it. Problem is for some reason sometimes when going to a particular "site" like... /somedevtraining/ - it fails but other variations/combinations works fine.

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.