将 ASP.NET MVC 混合到 ASP.NET WebForms 中
由于某种原因,我的路由忽略了任何访问我的 MVC 页面的尝试,只是给了我 404。我有一个 WebForms 应用程序设置如下:
Virtual Directory: thing
所以我通常像这样访问我的网站:
我的 ASP.NET WebForms 应用程序的原始结构镜像了文件系统,因此我有充满 .aspx 文件的文件夹,并且我需要能够像这样使用它们。由于某种原因,当我尝试使用 MVC 路由访问页面时,例如:
我刚刚收到 404 错误。我自己使用过 ASP.NET MVC,我知道即使我没有正确设置文件夹,我也不会收到 404 错误。我会得到找不到页面的原因以及提示文件应该在哪里。以下是我的路线信息。我哪里错了?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
For some reason my routing is ignoring any attempt to access my MVC pages and simply giving me 404s. I have a WebForms app set up like the following:
Virtual Directory: thing
So I usually access my site like so:
The original stucture of my ASP.NET WebForms app mirrors the file system so I have folders full of .aspx files and I need to be able to use them like that. For some reason when I try to access a page using the MVC routing such as:
I just get a 404 error. I have used ASP.NET MVC on it's own and I know that even if I didn't set up my folders properly, I wouldn't get a 404. I would get the reasons why the page couldn't be found and hints to where the files should be. Below is my routing info. Where am I going wrong?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能告诉我您正在运行的操作系统以及该站点是在 VS.NET Web Dev 服务器还是 IIS 下运行吗?
MVC 中的路由将请求定向到 Controller 类,然后定向到特定的 Action 方法。您是否有一个名为 HomeController 的类,其中包含一个名为 Index 的方法?
假设您有一个看起来像这样的控制器...
...那么您提到的网址应该可以工作。但是,ASP.NET MVC 希望在 vdir 下名为 Views\Home 或 Views\Shared 的文件夹中找到与 Home 控制器关联的任何视图。在这种情况下,对于 Index 操作,它将期望找到名为 Index.aspx(或 .ascx)的视图。但是,缺少视图通常不会导致 404 - 这通常是由于找不到控制器、找不到操作方法或在 IIS 6 上 asp.net 管道不在 vdir 的通配符设置中引起的。
更新:
您确定您的 web.config 已就位 MVC HttpHandler (以便 MVC 位于 ASP.NET 管道中)。 有这样的内容
您应该在您的
httpHandlers
部分中... ...在 web.config 的“httpModules”部分中。
更新 2:
根据您的评论,我怀疑您尚未在管道中获取 ASP.NET MVC 代码。您应该获取 web.config 并将其与新创建的 MVC 站点中的 web.config 进行比较,并查找缺少的配置项。我在上面建议了几个,但可能还有更多。
Can you tell me what OS you're running on and whether this site is running under VS.NET Web Dev server or IIS?
Routing in MVC directs a request to a Controller class and then a specific Action method. Do you have a class named HomeController with a method named Index?
Assuming you had a controller that looked this this...
... then the url you mentioned should work. However, ASP.NET MVC will expect to find any views associated with the Home controller in a folder named Views\Home or Views\Shared under your vdir. In this case, for the Index action, it will expect to find a view named Index.aspx (or .ascx). However, a missing view doesn't usually result in 404 - that's usually caused by the controller not being found, the action method not being found, or on IIS 6 the asp.net pipeline not being in the wildcard settings for the vdir.
update:
Are you sure your web.config has the MVC HttpHandler in place (so that MVC is in the ASP.NET pipeline). You should have something like this...
... in your
httpHandlers
section and this...... in your 'httpModules' section of web.config.
update 2:
Based upon your comments I suspect you've not got the ASP.NET MVC code in the pipeline. You should take your web.config and compare it with one from a freshly created MVC site and look for the missing config items. I've suggested a couple above, but there might be more.