xtremereality
asked on
asp.net mvc folder organization
Hi all,
I'm new on asp.net mvc 2.0. I have a simple question that seems to be a bit silly but acutally I don't know how to resolve this little issue.
MVC is driven by action rather than pages.
The default routing should be: controller/action/id
An example can be: http://www.foo.com/home/dosomething
What if I want to organize things in a different way?
Let's say for example I want my url like this:
http://www.foo.com/en/home/doSomething
or
http://www.foo.com/us/home/results/doSomething
What to do in this case?
Thanks
I'm new on asp.net mvc 2.0. I have a simple question that seems to be a bit silly but acutally I don't know how to resolve this little issue.
MVC is driven by action rather than pages.
The default routing should be: controller/action/id
An example can be: http://www.foo.com/home/dosomething
What if I want to organize things in a different way?
Let's say for example I want my url like this:
http://www.foo.com/en/home/doSomething
or
http://www.foo.com/us/home/results/doSomething
What to do in this case?
Thanks
You need to map your own routes.
In your global ASAX file
add
routes.MapRoute("home_exam ple","{cou ntry}/{con troller}/{ action}", new {controller = "Home", action = "DoSomething");
and create a method on your home controller
public ActionResult DoSomething(string country)
{
}
when ever you use /us/home/dosomething or /uk/home/dosomething the above method should be called with the relevant value in the country parameter.
the sky's the limit in how you use this, but remember that there is a potential performance penalty if it all gets too complex and there are loads of mapped routes.
In your global ASAX file
add
routes.MapRoute("home_exam
and create a method on your home controller
public ActionResult DoSomething(string country)
{
}
when ever you use /us/home/dosomething or /uk/home/dosomething the above method should be called with the relevant value in the country parameter.
the sky's the limit in how you use this, but remember that there is a potential performance penalty if it all gets too complex and there are loads of mapped routes.
ASKER
Thanks James my question is not necessary related with localization but more about routing in general,
If I'm understanding correctly, if I want this route:
http://foo.com/firstpart/secondpart/controller/action the only thing should be create this routes
routes.MapRoute("home_exam ple","{fir stpart}/{s econdpart} /{controll er}/{actio n}", new {controller = "Home", action = "DoSomething");
Is that correct?
If yes, this should not affect the real folder structure right? I mean I should have on VS this organization:
Controllers:
---------------->HomeContr oller.cs
Views:
---------------->Home
-------------------------> Index.aspx
and NOT
Controllers:
---------------->firstpart
-------------------------- -->secondp art
-------------------------- ---------- ---------> HomeContro ller.cs
And same thing with the Views?
If I'm understanding correctly, if I want this route:
http://foo.com/firstpart/secondpart/controller/action the only thing should be create this routes
routes.MapRoute("home_exam
Is that correct?
If yes, this should not affect the real folder structure right? I mean I should have on VS this organization:
Controllers:
---------------->HomeContr
Views:
---------------->Home
------------------------->
and NOT
Controllers:
---------------->firstpart
--------------------------
--------------------------
And same thing with the Views?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx