0

I'm trying to add MVC 4 into an existing webforms project. I followed this guide: https://www.packtpub.com/books/content/mixing-aspnet-webforms-and-aspnet-mvc

After adding to the web.config:

<add assembly="System.Core, Version=3.5.0.0, Culture=neutral,PublicKeyToken=B77A5C561934E089"/>
      <add assembly="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

I made a Controller (HomeController.cs) in the Controllers folder:

using System.Web.Mvc;

namespace MixingBothWorldsExample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "This is ASP.NET MVC!";
            return View();
        }
    }
}

I then added my view at Views/Home/index.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs"    Inherits="MixingBothWorldsExample.Views.Home.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <div>
        <h1><%=Html.Encode(ViewData["Message"]) %></h1>
    </div>
</body>

Then added my route:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
    routes.MapRoute("Test", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

For some reason, everything in my existing webforms project works just fine (including PageRoutes), however /home/index gives me a 404, implying that it's not even routing to the controller.

Anyone have any ideas?

maembe
  • 1,270
  • 1
  • 13
  • 25
  • Try either `runAllManagedModulesForAllRequests` or configure the `ExtensionlessUrlHandler` http://stackoverflow.com/questions/12495346/asp-net-4-5-mvc-4-not-working-on-windows-server-2008-iis-7 – Wiktor Zychla Oct 20 '14 at 16:17
  • Run a wireshark trace or open dev tools (F12) and tell us what the post data looks like. We can go from there.... – JWP Oct 24 '14 at 21:06
  • try the route debugger http://haacked.com/archive/2011/04/13/routedebugger-2.aspx/ – Venkatesh Oct 27 '14 at 14:12
  • URL routing capabilities were added to Web Form priojects only starting from ASP.NET 4 – Max Brodin Oct 27 '14 at 15:36
  • I tried out the Route Debugger. In the Route Data table it lists the following KVPs: controller=home and action=index It also say the mvc route matches the current request, yet I'm still getting a 404 – maembe Oct 27 '14 at 15:45
  • are you testing with the iis express or do you have published it to iis – kuma DK Oct 27 '14 at 16:11
  • I'm just using standard iis7 – maembe Oct 27 '14 at 21:39
  • Seems to be a problem with routes,did you added the RoutesModule in your web config? – vfabre Oct 28 '14 at 09:18

4 Answers4

1

Ultimately, I ended taking my existing website and adding it to an MVC application, rather than going the other way around. The routing worked just fine then.

maembe
  • 1,270
  • 1
  • 13
  • 25
0

Try this,

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute("Test", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
Venkatesh
  • 1,204
  • 1
  • 10
  • 18
0

I think you might need to add an id parameter to the Index method in HomeController. Even though it's optional, the router is looking for a method that takes an id.

Edit: Sorry, I don't know what I was thinking. This is wrong as demonstrated by the article you reference.

dae721
  • 86
  • 5
0

Can you try out the following routing rules. This works for me in MVC4 projects. This defines what is the default page that will load when you navigate to some url and port like locallhost:4897

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}
kuma DK
  • 1,812
  • 1
  • 18
  • 36