我应该将每个 MVC 视图命名为 Index.aspx 吗?

发布于 2024-07-12 03:00:17 字数 1040 浏览 5 评论 0原文

如果我没记错的话 - ASP.NET MVC 的约定似乎希望我对控制器视图执行以下操作。

今天,我创建了“产品”目录,并将“索引”视图放入其中。 然后,我创建一个“ProductsController”并在其上创建一个返回视图的“Index”方法。 仅返回不带参数的 View() 将去获取“Index.aspx”页面,因为它与该方法同名。

 public class ProductsController : Controller
    {
        public ActionResult Index()
        {
            return View();   // looks for Index.aspx in Products directory
        }
    }

现在这样就好了。 但我最终会得到十亿个 Index.aspx 页面,而且我是从不关闭任何文件的人之一,所以我最终会发疯。

或者,我可以创建 Products/Products.aspx 并将我的控制器更改为以下内容:

 public class ProductsController : Controller
    {
        public ActionResult Index()  // my default routing goes to Index (from sample project)
        {
            return View("Products");
        }
    }

我了解其工作原理,并且在 MVC 设计模式中完全可以做到这一点。 它不是黑客或类似的东西。

我的问题(听完 这个 PDC 视频后)是 MVC 中更倾向于约定而不是可定制性(或者任何正确的短语是什么)。

所以我想知道我是否缺少第三种方法,或者人们是否对 Visual Studio 中的 50 个索引选项卡感到满意?

If I'm not mistaken - the conventions of ASP.NET MVC seem to want me to do the following for a controller view.

Thats to day I create 'Products' directory into which I place my 'Index' view. I then create a 'ProductsController' and create an 'Index' method on it which returns a View. Returning just View() with no arguments will go and fetch the 'Index.aspx' page because its the same name as the method.

 public class ProductsController : Controller
    {
        public ActionResult Index()
        {
            return View();   // looks for Index.aspx in Products directory
        }
    }

Now thats just fine. BUT I'll end up with a billion Index.aspx pages, and I'm one of these people that never closes any files so I'll end up going crazy.

Alternatively I can create Products/Products.aspx and change my controller to the following :

 public class ProductsController : Controller
    {
        public ActionResult Index()  // my default routing goes to Index (from sample project)
        {
            return View("Products");
        }
    }

I understand how that works, and that its completely fine within the MVC design pattern to do this. Its not a hack or anything like that.

My problem (after listening to this PDC video) is that convention over customizabiltity is favored in MVC (or whatever the correct phrase is).

So I'm wondering if I'm missing a third way, or if people are just fine with 50 Index tabs in Visual Studio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百思不得你姐 2024-07-19 03:00:17

让每个控制器的默认操作具有相同的名称只会简化路由(检查 global.asax)。 此外,所有事物(产品、书籍、联系人等)都使用具有相同名称的操作/视图来实现相同的功能,然后代码变得更容易导航和理解。 当作为团队的一部分工作时,这种约定的使用尤其重要,因为它将鼓励开发人员之间保持一致的代码。

在查看另一个问题时,我在Codeplex 上的 MVCContrib 项目。 这可能会给你一些想法。

Having the default action for each controller have the same name just simplifies the routing (check global.asax). Also, it all things (Products, Books, Contacts, ...) use actions/views with the same name for the same function, then the code becomes much easier to navigate and understand. This use of convention is especially important when working as part of a team as it will encourage consistent code across developers.

While looking at another question, I ran across SimplyRestfulRouting in the MVCContrib project on codeplex. This might give you some ideas.

君勿笑 2024-07-19 03:00:17

我认为你应该在操作之后命名方法并命名视图(如果它有意义并且它不在操作之间共享,与操作相同)。 您可能应该更改您的路由机制,因为 Index 并不是真正的描述性名称。 操作名称应该代表它的作用(就像任何方法一样),并且不应该硬编码为 Index 或类似的内容。 应改为编辑路由。

I think you should name the method after the action and name the view (if it makes sense and it's not shared between actions, the same as the action). You should probably change your routing mechanism as Index isn't really a descriptive name. The action name should represent what it does (just like any method) and shouldn't be hardcoded to Index or something like that. Routing should be edited instead.

硬不硬你别怂 2024-07-19 03:00:17

这只是一个模式。 使用该模式,但要让它为您服务。 就我个人而言,我喜欢让我的视图按其功能命名,并且我没有很多 Index.aspx 页面,因为我没有很多索引。

It's just a pattern. Use the pattern, but make it work for you. Personally, I like to have my Views named by their function and I don't have many Index.aspx pages, because I don't have many indices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文