ASP.NET MVC 中的约定优于配置
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你回答了你自己的问题。
没有硬性规则阻止您为视图调用非常具体的名称,例如“ListOfFooBars”或“EditFizzBuzz”或“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.