views:

199

answers:

2

I am very confused the View and corresponding controller has to be set in MVC 1.0 project structure. Currently in a default application we have About.aspx page under Home folder and all the controller action is handled in HomeController. This controller mixes up Home action and About action. It makes things messy. I like to have clear separation of my controller. Like to have one About Controller and HomeCotroller separately. For it I have create another folder "About" under view folder and place Aboput.aspx in it , otherwise we will get the error below. How can I achieve it ?? I like to have exact folder structure as in View, same as in Cotroller.

The view 'About' or its master could not be found. The following locations were searched:
~/Views/About/About.aspx
~/Views/About/About.ascx
~/Views/Shared/About.aspx
~/Views/Shared/About.ascx 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The view 'About' or its master could not be found. The following locations were searched:
~/Views/About/About.aspx
~/Views/About/About.ascx
~/Views/Shared/About.aspx
~/Views/Shared/About.ascx

I like to have folders like \iew\About.aspx and Cotroller\AboutController.cs or \View\Info\About.aspx and \Controller\Info\AboutController.cs.

This will make my project and code very cleanly placed and good to maintain. Thanks in advance for helping

+2  A: 

I think they would want you to have

Views/About/Index.aspx

which /Views/AboutController Index() would point to

Routing Example:

in Global.asax:

protected void Application_Start()
{
    MyRoutes.RegisterRoutes(RouteTable.Routes);         
}

then in the MyRoutes class:

public class MyRoutes
{
     internal static void RegisterRoutes(RouteCollection routes)
     {
         // add routes
     }
}
hunter
You could always add a Route that would point "/About.aspx" to "/Views/Home/About.aspx"
hunter
Yes, I can do this, But in case of large projects, my global.asx.cs file will be very big. Any better alternative ?
Jit
Once you come up with all of the patterns you would need it's not so bad. Also, you don't have to put all of this into the global.asax. I put an example of using another class to register your routes.
hunter
+1  A: 

You should probably look at the basic tutorials for the ASP.Net MVC framework. It operates under several conventions - and one of the biggest conventions is the file naming and folder structure.

While the structure you like may be good for simple projects or your personal preference, it's not ideal for large projects. You need to either follow the convention, or learn enough about the framework so you can override the behavior of it.

womp
Actually I am thinking on Large project point of view. Let's say I have a folder Sales under view and then I have 10 .aspx files(One.aspx,Two.aspx...Ten.aspx) under sales folder. This case I have to implement all the controller logic under SalesController.cs file. Just think of how wrong practice is it ? If it gives the option like OneController.cs, TwoController.cs then it will be much better of clear separation of logic.Any idea to solve this issue?
Jit
That's a very, very bad separation of logic. You're proposing a convention where the requested view file is dictating the controller instantiation, which is exactly opposite of how MVC should work.
womp
Womp,If I agree with you, then you are telling me to accommodate all the view's controller logic in a single controller file? How do I say that my controller is creating a view? Actually a single controller is creating multiple views(related logically but physically separate, I may not be a single time, but i contain the code to control multiple views). I could not understand how it violates the MVC framework concept? A lot of applications before MVC is released has been developed with one to one Controller and view map pattern. Can you explain me how justify the MVC pattern here ?
Jit
Perhaps I misunderstood your intent - it sounded like you wanted to have a request to a view file (One.aspx) be routed to a controller, OneController.cs. MVC should have controllers that are loosely based on your entities, which would have multiple views. While there's nothing wrong with a one-to-one mapping as long as the controller is dictating the view, having the view dictate the controller is backwards.
womp
I think we are in same line now. But how to achieve it in MVC1.0 ? To have this type of feature, I have to Create view structure like \View\One\One.aspx and Controller as \Controller\OneController.cs. This is ok. But I like to achieve the above feature by placing my controller as \Controller\Sales\OneController.cs and view as \View\Sales\One.aspx. It will give a clear cut folder structure. For it I think we need to change some configuration things. Any idea?
Jit