0

I'm building a website for multiple organizations. For example, people could go to mydomain.com/org1/home/index or mydomain.com/org2/home/index.

What I am trying to do is have the following routing,

mydomain.com/{area}/{controller}/{action}/{id}

I can't figure out how to do this, and frankly not even sure how to even start setting up my routing. I want to be able to access {area} string in order to decide which images to display, what text etc.

I hope I'm making sense with what I'm trying to do.

1 Answer 1

0
routes.MapRoute(
name: "Default",
url: "{area}/{controller}/{action}/{id}"
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
area = UrlParameter.Optional
);

Your Home controller will look something like this:

public ActionResult Index(string area, string id)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for the quick response! That definitely solves that problem. And now I'm faced with another since this is not always the case, i.e. having the {area} parameter. So now, when I go to /Account/Login it gives error. If I add a string before like this: /foo/Account/Login it takes me to the login page. I realize this is another problem. What I did is I used the route you gave me as the first "customRoute" and then the default route is declared after that. Do you know how to solve this? Or should I perhaps post another question?
You can add routes.MapRoute( name: "Account", url: "{controller}/{action}/{id}" defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional ); just before the "Default" route up top
So now I have two routes, "Default" and "Account". If "Account" route is up top, I'm not able to access mydomain.com/foo/home/index even if HomeController has Index(string area), it simply gives me an error. If I place "Default" up top, I am able to access mydomain.com/foo/home/index, works great but I am also able to access other controllers/actions where I don't want to. For example, I have a DivisionsController with ActionResult Index(). Now I am able to call mydomain.com/foo/divisions/index and it returns the same page as if I wrote mydomain.com/divisions/index. I don't want that...
Have a look at this solution:
That seems to be exactly what I'm after. Thanks a lot for your help! I voted you up but I'm too young in here for it to count :)

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.