Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding MVC to a ASP.NET Website

I'd like to add MVC support to an existing Website project (not a Web application project) and have a few questions.

  1. To get Visual Studio's MVC goodness, it appears that I need to update the .csproj file's <ProjectTypeGuids> node. There is no csproj file in a website project.
  2. It appears that I need to create Models and Controllers folder in the App_Code. Is this true? I prefer having these folders under root.
  3. For Views should I be creating a aspx and aspx.cs files? Is cshtml razor files supported in this kind of a setup?

Any other responses are appreciated. Thanks

like image 426
Nick Avatar asked Aug 25 '11 17:08

Nick


People also ask

Does .NET use MVC?

ASP.NET gives you a powerful, patterns-based way to build dynamic websites using the MVC pattern that enables a clean separation of concerns.

Can I use MVC controller as Web API?

In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below. Then give it a suitable name and click OK.

What is MVC in ASP.NET with example?

MVC is a pattern known as Model-View-Controller. It is made up of three pieces; Model, View, and Controller that interact with each other to provide capabilities to the application that implements it. MVC is related to ASP.NET in much the same way as it is to an application.

What is MVC and how it is used to create websites?

MVC - allows developers to create websites or specific pages using the Model-View-Controller architectural pattern (based on the ASP.NET MVC framework). Working with this model requires knowledge of programming and ASP.NET MVC.


1 Answers

With asp.net MVC2 and above, the MVC team separated the core functionality into three different assemblies, each of which extends from the common System.Web assembly:

  • System.Web.Routing
  • System.Web.Abstractions
  • System.Web.Mvc

With this seperation, they went ahead and made the assemblies to "work in Medium-trust server enviroments and be bin-deployable".

One of the good things about this featuere is, you don't have to have a specific project type to run MVC. You only need the assemblies, some directories and a tweaked web.config.

To do this, you need only to place the assemblies in your local bin folder of your project and make the necessary references for those assemblies. Once this is done, you have access to asp.net MVC.

Here are some detailed instructions from the Wrox Professional ASP.NET MVC 1.0 book which should help you get started:

Including MVC in Existing Web Forms Applications

Adding ASP.NET MVC functionality to an existing Web Forms application is comprised of three different steps:

1. Add a reference to the three core libraries that ASP.NET MVC needs: System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions.

2. Add two directories to your application: Controllers and Views.

3. Update the Web.config to load the three assemblies at run time as well as registering the UrlRoutingModule HttpModule.

For reference, here are a couple of blogs/sites which have some more detailed scenarios which might help you out:

Mixing ASP.NET Webforms and ASP.NET MVC

ASP.NET WebForms and ASP.NET MVC in Harmony

Good luck, and hope this helps you out some.

like image 107
Chris Avatar answered Oct 06 '22 07:10

Chris