Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • http://localhost/thing/someFile.aspx
  • http://localhost/thing/someFolder/anotherFile.aspx

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:

  • http://localhost/thing/Home/Index

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);
}
like image 498
Brian David Berman Avatar asked Nov 20 '09 20:11

Brian David Berman


People also ask

Can MVC and ASP.NET coexist?

MVC is just a different implementation of the IHttpHandler interface so both classic ASP.NET and ASP.NET MVC pages can coexist in the same app. Show activity on this post. As you've probably noticed with the above answers, yes this is very possible to do.

Is ASP.NET Web Forms dead?

Does this mean ASP.NET Web Forms is dead and should no longer be used? Of course not! As long as the . NET Framework ships as part of Windows, ASP.NET Web Forms will be a supported framework.

What is the difference between ASP.NET Web Forms and ASP.NET MVC?

Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.

Can we add ASPX page in MVC?

If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.


1 Answers

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

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

like image 109
Martin Peck Avatar answered Oct 22 '22 09:10

Martin Peck