ASP.NET MVC 是否在根路径中搜索与控制器同名的文件夹?

发布于 2024-10-13 12:21:49 字数 665 浏览 2 评论 0原文

我可能遇到了 ASP.NET MVC 中的错误,但我不确定(在搜索解释后)我是否就在这里。

ASP.NET MVC 非常重视“约定优于配置”,如果他们坚持的话,这很好。

我创建了一个名为“ChatController”的控制器,并在项目的根路径中创建了一个名为“Chat”的文件夹,以保存与聊天相关的 ViewModel。

然后我在“视图”中创建了一个名为“Chat”的文件夹文件夹,因为众所周知,ASP.NET MVC 将在“Views/[ControllerName]”文件夹和“Views/[Shared]”文件夹中查找匹配的视图。

但每次我尝试点击我的视图时,我都会得到“404”。我估计。天啊,也许我把一些简单的事情搞砸了。所以我复制了一些有用的东西,然后重新命名。不..仍然是404。

然后我想..但是..它不可能...不,不可能是我在根目录中有一个文件夹被称为与我的Views/文件夹中相同的文件夹?可以吗?

我重命名了...宾果游戏..我的视图受到了影响,一切正常。

当我说这违背了 ASP.NET MVC 的工作方式时,我错了吗?它不是应该只在 Views/[ControllerName] 和 Views/Shared 中查找合适的视图吗?

如果我错了,请指出一些有关该主题的文献(如果您知道的话)...对我来说,这确实看起来像一个错误。

提前致谢!

I might have come across a bug in ASP.NET MVC, but I'm not sure (after searching for an explanation) if I am right here.

ASP.NET MVC weighs heavily on "Convention over Configuration", which is nice, if they stick to it.

I created a controller called "ChatController", and I made a Folder in the Root-path of my project called "Chat", to keep my Chat-related ViewModel in.

Then I created a folder called "Chat" in my "Views" folder, because as we all know, ASP.NET MVC will look in the "Views/[ControllerName]"-folder and "Views/[Shared]"-folder for a matching view.

But every time I tried to hit my View, I got "404". I figured. Hell, maybe I messed something simple up. So I copied from something that worked, and just renamed. Nope.. still 404.

Then I figured.. but.. it couldn't be... NOOO, it couldn't be that I have a folder in the root being called the same as in my Views/ folder? could it ?

I renamed... And Bingo.. my View got hit, and everything worked.

Am I wrong when I say that this is against how ASP.NET MVC is supposed to work. Isn't it supposed to look ONLY in Views/[ControllerName] and Views/Shared for a suitable View?

If I'm wrong, please point to some literature on the subject (if you know any)... This really looks like a bug to me.

Thanks in advance!

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

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

发布评论

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

评论(1

请远离我 2024-10-20 12:21:49

这与 ASP.NET 的工作方式而不是 ASP.NET MVC 更相关。当您的根目录中有 Chat 文件夹并且请求 /Chat 时,Web 服务器始终优先于路由引擎提供物理文件夹服务。因此,它在您的 /Chat 文件夹中查找一个名为 Default.aspx 的文件(或您设置的任何默认文档),但找不到,并且失败了404.

在 ASP.NET 4.0 中,您可以放松: -)

<httpRuntime relaxedUrlToFileSystemMapping="true" />

This is more related to how ASP.NET works rather than ASP.NET MVC. When you had Chat folder in your root and you requested /Chat the web server always prefers serving physical folders before the routing engine. So it looked for a file called Default.aspx (or any default document you've setup) in your /Chat folder, it couldn't find one and it blew with 404.

In ASP.NET 4.0 you can relax it :-)

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