将ASP.NET MVC混合到ASP.NET WebForms中

时间:2022-04-07 05:25:45

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:

出于某种原因,我的路由忽略了任何访问我的MVC页面的尝试,只是给我404。我有一个WebForms应用程序设置如下:

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:

我的ASP.NET WebForms应用程序的原始结构镜像文件系统,所以我有完整的.aspx文件夹,我需要能够像这样使用它们。出于某种原因,当我尝试使用MVC路由访问页面时,例如:

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?

我刚收到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);
}

1 个解决方案

#1


2  

Can you tell me what OS you're running on and whether this site is running under VS.NET Web Dev server or IIS?

你能告诉我你在运行什么操作系统,以及这个站点是否在VS.NET Web Dev服务器或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?

MVC中的路由将请求定向到Controller类,然后定向特定的Action方法。你有一个名为HomeController的类,其方法名为Index吗?

Assuming you had a controller that looked this this...

假设你有一个控制器看起来这个...

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

... 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.

...那么你提到的网址应该有效。但是,ASP.NET MVC期望在vdir下的名为Views \ Home或Views \ Shared的文件夹中找到与Home控制器关联的任何视图。在这种情况下,对于Index操作,它将期望找到名为Index.aspx(或.ascx)的视图。但是,缺少视图通常不会导致404 - 这通常是由于找不到控制器,找不到操作方法,或者IIS 6上的asp.net管道不在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...

您确定您的web.config具有MVC HttpHandler(因此MVC位于ASP.NET管道中)。你应该有这样的东西......

<add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

... in your httpHandlers section and this...

...在你的httpHandlers部分和...

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

... in your 'httpModules' section of web.config.

...在web.config的“httpModules”部分中。

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.

根据您的意见,我怀疑您没有在管道中获得ASP.NET MVC代码。您应该使用web.config并将其与新创建的MVC站点中的一个进行比较,并查找缺少的配置项。我已经建议了几个,但可能会有更多。

#1


2  

Can you tell me what OS you're running on and whether this site is running under VS.NET Web Dev server or IIS?

你能告诉我你在运行什么操作系统,以及这个站点是否在VS.NET Web Dev服务器或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?

MVC中的路由将请求定向到Controller类,然后定向特定的Action方法。你有一个名为HomeController的类,其方法名为Index吗?

Assuming you had a controller that looked this this...

假设你有一个控制器看起来这个...

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

... 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.

...那么你提到的网址应该有效。但是,ASP.NET MVC期望在vdir下的名为Views \ Home或Views \ Shared的文件夹中找到与Home控制器关联的任何视图。在这种情况下,对于Index操作,它将期望找到名为Index.aspx(或.ascx)的视图。但是,缺少视图通常不会导致404 - 这通常是由于找不到控制器,找不到操作方法,或者IIS 6上的asp.net管道不在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...

您确定您的web.config具有MVC HttpHandler(因此MVC位于ASP.NET管道中)。你应该有这样的东西......

<add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

... in your httpHandlers section and this...

...在你的httpHandlers部分和...

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

... in your 'httpModules' section of web.config.

...在web.config的“httpModules”部分中。

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.

根据您的意见,我怀疑您没有在管道中获得ASP.NET MVC代码。您应该使用web.config并将其与新创建的MVC站点中的一个进行比较,并查找缺少的配置项。我已经建议了几个,但可能会有更多。