开发者

ASP.NET MVC 2: View Subfolders?

开发者 https://www.devze.com 2023-01-31 12:19 出处:网络
It seems to me that as I\'m working with View and Controllers, that Controllers only handle the first level of their respective folder.

It seems to me that as I'm working with View and Controllers, that Controllers only handle the first level of their respective folder.

/Controllers/MembersController
/Views/Members/

How is the Controller supposed to handle sub-fol开发者_JAVA技巧ders?

/Views/Members/Business


The Controller isn't. If you want a page at the url /Views/Members/Business/ThePage, you'll solve that with a route definition in your global.asax.cs. If you need to put views in subfolders for organization, you should consider splitting up your controllers.


Update in response to your comment.

In short: yes, the routing definitions are responsible for choosing what controller should handle your request.

It seems that a slightly more detailed walkthrough of how the MVC framework was designed to be used might be in place. I'll keep this really basic, and skip all the stuff about the framework's internal workings. (That will make some advanced readers think I'm saying stuff that's flat out wrong. Please bear with me - I'm just being intentionally sloppy...) Here goes...

  1. When a request is submitted from a browser to your web server, the MVC framework goes to the route definitions to see where the request should be sent. It looks through them in the order you define them, top first, so if there are several matches only the first one will be relevant. (Therefore, you want to keep your very general routes, like the default one, at the bottom.)

  2. When the framework has decided that a route is a match, it looks up what controller and action the url should be routed to. It fills in all action parameters with data from the url, and calls the action method.

  3. The action method on the controller is now responsible for being "the spider in the center of the web", that instruments everything that needs to be done to serve the response. This might include querying your model for data, calling various library methods for calculations or almost anything else. The final step of the controller method is determining what response should be returned - in most basic cases, the response should be a view, and the code for returning it is return View();.

  4. The view (or whatever other result you're returning) possibly gets some data from the controller, and is then responsible for rendering this data correctly. When the view has done its job, the framework serves it back to the client.

As you can see, the decision on what controller action should be called is much earlier (step 1 and 2) than the decision on what the return result should be (end of step 3), and the two of them aren't even necessarily related to each other.

To reflect this in your application, you want to have a folder and file structure for controllers and views that corresponds to the available controllers and their action methods (at least those of them that can return a ViewResult), and a route collection that reflects what urls you want your user to use to get to these controller actions. By defining more routes than the default one, you can get quite a variety of url structures, without changing your controller/action method/view structure at all.


If all of this still seems like a labyrinth of decisions, it might be appropriate to go to http://www.asp.net/mvc/ and watch some of the learning videos or read some tutorials. There are some really good both videos and texts there about how the framework works, and how it's intended to be used.


In your controller action, you can return a view from any folder you want like this:

return View("/Views/Members/Business/Index.aspx", model);


If anyone is still interested in a solution, MVC has a thing called "Areas" that allow you to define subfolders.

http://msdn.microsoft.com/en-us/library/ee461420%28v=VS.100%29.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号