Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine ASP.Net MVC with WebForms

Is it possible to create a MVC root application (Portal with masterpages and themes) and add a couple of WebForms based subprojects (we already have an existing WebForms application that we would like to integrate into the Portal)?

How would you centralize navigation (sitemaps, url routing)?
How would you share the masterpages?
How would you refer to resources (~ issues etc.)?

like image 289
Zyphrax Avatar asked Feb 04 '10 21:02

Zyphrax


People also ask

Can you mix Web Forms and MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.

Can we use 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.

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.

When should I use ASP.NET MVC vs Web Forms?

Asp.Net MVC has Partial Views for code re-usability. 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.


3 Answers

Combining web forms with MVC is entirely possible. See this blog post by Scott Hanselman for an introduction.

  • Sharing master pages: see this StackOverflow question
  • routing: In ASP.Net 4.0, routing has been enabled for web forms page routes (scottgu's blog)
like image 100
jeroenh Avatar answered Oct 03 '22 17:10

jeroenh


For reference I managed to accomplish this task yesterday - add MVC functionality to an existing WebForms website, that is - so thought I would write in case it is of any use to anybody.

People say: "the best way is to rewrite your web forms application as MVC" but there is often no business case for this when the application is well established and has grew over time to be quite a monster (that still works well I might add). In my case, we had been asked to implement a new function into this application - and having been developing in MVC on other projects recently - I wanted to build this new function in MVC. This is what I did:

  1. Create a new ASP.NET MVC 4 Application and selected the Empty Template (with Razor)

  2. Once loaded, right-clicked References > Browse and then navigated to the Bin folder of my existing website. I then added in the DLL's I was using in the existing site. For reference, I am using AjaxControlToolkit so decided to add in the latest one from NuGet. This should then auto-update the web.config file

  3. I then opened up Windows Explorer and navigated to the root folder of my existing website. Using Visual Studio, I then replicated the top-level folder structure of the existing site (minus the Bin folder) resulting in the 2 sites having the same top-level folders (but the new project having the MVC App_Start, Controllers, Models and Views folders also, but not Bin obviously).

  4. Once setup, I copied the sub-folders and files from the corresponding top-level folder in Windows Explorer and pasted them into the solution. If you do it the way, the project allows you to copy in sub-folders and content directly, adding existing item via visual studio only copies in files thus saving time. Please note: for the root folder, I did NOT copy in web.config or Global.asax, but copied in all other files (like default.aspx, etc)

  5. Once all content was copied, I opened up web.config in the new project and web.config in the existing site. I copied into the new web.config anything specific to the existing website (appSettings, connectionStrings, namespaces, membership info, etc). The same applied to the Global.asax files.

  6. I attempted a build and fixed anything I'd missed.

  7. Once everything was right (and I had shot down a few Yellow screens of death) I noticed that I got the page not found error when loading default.aspx. This is down to routing, here is the changes I made to the Routing info (located in Global.ascx MVC 3, or App_Start > RouteConfig.cs MVC 4):

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        // This informs MVC Routing Engine to send any requests for .aspx page to the WebForms engine
        routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.aspx");
    
        // Important change here: removed 'controller = "Home"' from defaults. This is the key for loading
        // root default page. With default setting removed '/' is NOT matched, so default.aspx will load instead
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional }
        );
    }
    

With all these steps taken, the site loaded and ran as it always did, but I now have a ASP.NET WebForms and MVC hybrid. I am sure there are probably easier ways of accomplishing this task, but I tried other methods and failed. This way took me around 20 minutes and I was up and running... well sat and coding :)

like image 36
Matt Avatar answered Oct 03 '22 19:10

Matt


Great answer above. The other thing you could consider is migrating from ASP.NET to ASP.NET MVC, in which case this post might help you migrate the project files.

like image 32
Odd Avatar answered Oct 03 '22 18:10

Odd