ASP.NET MVC 中的约定优于配置

发布于 2024-07-25 18:46:15 字数 535 浏览 4 评论 0原文

我对 ASP.NET MVC 比较陌生,迄今为止该平台的清晰度给我留下了深刻的印象。 然而,有一个方面我觉得不舒服。

起初,我接受了这样一个事实:当我说

return View();

我正在调用一个返回 ActionResult 的辅助方法时,并对要呈现的视图、路由值等做出一些假设。但最近我一直在编写看起来更像这样的代码:

return View("Index", new { id = myID })

因为通过阅读这一行代码,我立即清楚发生了什么。

最近,我一直在努力解决以下问题:我可以在选项卡上打开一个 Index.ASPX 视图,但我无法立即判断它来自哪里,因为 IDE 不会突出显示当前选项卡在对象资源管理器中。 我没有将文件名更改为 ControllerNameIndex.ASPX,但我确实在视图中放置了一个更具体的标题。 不过,这并没有多大帮助。

您如何处理这些含糊不清的情况?

I am relatively new to ASP.NET MVC, and am very impressed with the clarity of the platform so far. However, there is one aspect that I find uncomfortable.

At first, I accepted the fact that when I say

return View();

I am calling a helper method that returns an ActionResult, and makes some assumptions about which view to present, route values, etc. But lately I have been writing code that looks more like this:

return View("Index", new { id = myID })

because it is immediately clear to me what's happening by reading that single line of code.

Lately I have been struggling with the fact that I can have an Index.ASPX view open on the tabs, and I can't immediately tell where it comes from because the IDE doesn't highlight the current tab in the Object Explorer. I haven't resorted to changing the names of the files to ControllerNameIndex.ASPX, but I do put a title in the view that is more specific. Still, it doesn't help much.

How do you deal with these kinds of ambiguities?

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

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

发布评论

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

评论(1

无法言说的痛 2024-08-01 18:46:15

我想你回答了你自己的问题。

没有硬性规则阻止您为视图调用非常具体的名称,例如“ListOfFooBars”或“EditFizzBu​​zz”或“AddNewGeeblup”。 默认视图引擎的命名约定仅指定视图下有一个与您的模型名称对应的文件夹,并且该文件夹下有一个与您的视图名称对应的 ASPX 或 ASPC 文件。

I think you answered your own question.

There's no hard rule preventing you from calling your views very specific names, such as "ListOfFooBars" or "EditFizzBuzz" or "AddNewGeeblup". The naming convention for the default view engine only specifies that there's a folder corresponding to your model name under views, and there's an ASPX or ASPC file under that folder that corresponds to your view name.

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