Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ASP.NET MVC Area to ASP.NET Web Forms existing project

I have a website developed in ASP.NET Web Forms with .NET FrameWork 4.5.1

We have a requirement to build a replica of the site for mobile devices (responsive doesn't work for us in this case) so I ended up thinking of doind this part of the site with ASP.NET MVC.

Note before you continue: I know there are a few questions answered about this topic and in the web there are some articles on how to mix them. But none of them works for me because I'm trying to use the One ASP.NET Framework to avoid going through a lot of manual work.

So, what I did was:

  1. Add the Nuget Package for MVC in my ASP.NET WebForms project
  2. Once added, I configured an MVC Area to contain my Mobile site in MVC

The project structure looks like this:

Project Structure

And here comes my problem: If I run any of the ASPX pages, they run ok. When I go to the /Mobile/Home/Index it says the resource is not found. I suspect this is because the App_Start folder doesn't exists in my project and Starup.cs isn't there either so the website doesn't know anything about MVC routing.

Question

  1. How can I add the App_Start folder and the Starup.cs with defaults values without suffering the process (avoiding copy/paste or manual processes)?

  2. How can I make this both (web type projects) live together as they say it can be done with the One ASP.NET Framework?.

Thanks!

like image 317
Felipe Correa Avatar asked May 12 '15 17:05

Felipe Correa


People also ask

How do I add a MVC to an existing webforms project?

To do this, right click you project and click Add> New Scaffolded Item… This will bring a dialog to choose the Controller class like below. Click “MVC 5 Controller – Empty”. Visual Studio will download and configure all the items for you to start working on MVC.

Can we plug an ASP.NET MVC into an existing ASP NET application?

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.

How can add area in ASP.NET MVC project?

You can create an area by right-clicking on the project in the solution explorer -> Add -> Area.. , as shown below. Enter the name of an area in the Add Area dialogue box and click on the Add button. This will add an admin folder under the Area folder, as shown below.

How do I add a Web API to an existing webforms project?

Create the Model and ControllerIn Solution Explorer, right-click the project. Select Add New Item. Under Installed Templates, expand Visual C# and select Web. Then, from the list of templates, select Web API Controller Class.


1 Answers

You'll need to register the areas in your Global.asax (add it through the Add Item menu option if it does not exist):

public class Global : HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();

    }

}

This will enable the link to your Mobile @ localhost/{ProjectName}/Mobile/Home

like image 179
C Bauer Avatar answered Oct 08 '22 21:10

C Bauer