ASP.NET MVC 2:查看子文件夹? [英] ASP.NET MVC 2: View Subfolders?

查看:88
本文介绍了ASP.NET MVC 2:查看子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,当我与视图和控制器的工作,即控制器只处理各自的文件夹中的第一级。

  /控制器/ MembersController
/查看/会员/

控制器应该如何处理子文件夹?

  /查看/会员/业务


解决方案

控制器的不是。如果你想在一个页面中的网址 /浏览/成员/商务/翻动书页,你解决了路由定义在你的Global.asax.cs。如果你需要把子文件夹中的观点为组织,你应该考虑分裂你的控制器。


更新响应您的评论。

在短:是的,路由定义,负责选择哪些控制器应处理您的要求。

似乎的MVC框架是如何设计的稍微更详细的演练,以用于可能在的地方。我会记住这非常基本的,并跳过所有关于该框架的内部运作的东西。 (这将使一些先进的读者认为我说的东西,这平了错误的。请多多包涵 - 我只是故意马虎...)这里去...


  1. 当一个请求从浏览器提交给Web服务器,MVC框架转到路由定义,看看那里的应该发送请求。它看起来通过他们在你第一次定义它们,上面的顺序,所以如果有多个匹配只有第一个将是相关的。 (因此,你要保持你的很一般的路线,像默认的,在底部。)


  2. 在框架已决定路由匹配,它看起来了一下控制器和动作的URL应该被路由到。它填补了从URL数据中的所有动作参数,并调用操作方法。


  3. 控制器上的操作方法是现在为是在幅材的中心的蜘蛛,即,需要的仪器都做以服务响应负责。这可能包括查询您的数据模型,要求计算,抑或是其他任何不同的库法。控制器方法的最后一步是确定什么反应,应返回 - 在最基本的情况下,答案应该是观点,而code,返回是返回查看();


  4. 视图(或任何其他的结果你回来)可能从控制器得到了一些数据,然后负责正确显示这些数据。当视图已完成其工作,该框架提供其返回给客户端。


正如你所看到的,在什么控制器动作应该叫这个决定是要早得多(步骤1和2)比他们两个什么返回的结果应该是(步骤3月底)的决定,并AREN吨甚至必然的相互关系。

要在应用程序中反映了这一点,你想为控制器和视图的文件夹和文件结构对应于可用的控制器和动作方法(至少那些他们可以返回的ViewResult )和路由集合反映你希望你的用户使用来获得这些控制器动作什么的URL。通过定义比默认的多条航线,你可以得到很多种URL的结构,在不改变所有控制器/操作方法/视图结构。


如果这一切还似乎是决定一个迷宫,它可能是适当的去 http://www.asp.net / MVC / 并观看一些视频学习或阅读一些教程。有一些非常好的两个视频和​​文本那里大约框架是如何工作的,以及它如何打算使用。

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-folders?

/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.

这篇关于ASP.NET MVC 2:查看子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆