English 中文(简体)
Mixing ASP.NET MVC into ASP.NET WebForms
原标题:

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);
}
最佳回答

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

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 ViewsHome or ViewsShared 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...

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

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

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签