Adding MVC to a ASP.NET Website - asp.net

I'd like to add MVC support to an existing Website project (not a Web application project) and have a few questions.
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.
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.
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

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.

I've successfully added a ASP.NET MVC 3 to Webforms project and here are some suggestions:
Add Controllers as a separate class library project and add a reference to this project from the Web forms web project
I attempted to get VS MVC support (like goto controller etc), but adding the GUID {E53F8FEA-EAE0-44A6-8774-FFD645390401} to the .csproj file didn't help.
Yes, you can add references to get the Session from your class library. You can always mock it out if you want to write unit tests.
Add Views folder to the root
Add Models within App_Code
If you are using Razor, then you need to add System.Web.Razor and System.Web.WebPages references
If you are using Razor, update the Web.config per this post
Keep in mind, you can add server controls to your view as long as they don't use postbacks (I had a lot of server controls from my webforms project that I had to use)

I believe if you set up a new MVC project and copy your web forms across to the new project, they will render as expected.
I haven't experimented with this too much but I have tried in the past out of curiosity and the web forms were still rendered OK. I guess it depends on the complexity of your project as to whether this approach would work.
This would involve changing your project type.

I have seen this work in the past if you place a Global.asax file in the root of your website. You'll need a way for your project to recognize and differentiate MVC requests from standard requests, like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Default",
"{controller}.mvc/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
So, an MVC url in your app might look like: http://www.mywebsite.com/mycontroller.mvc/View/5

I was tasked with updating (but unfortunately not re-writing) a legacy .Net 3.5, VB.NET (ughh) webforms, web site project and I successfully upgraded to 4.0 and added MVC3 support (with seperate code compliation folders to support C#, yah!), and it works just fine.
The #DotnetDude instructions do work, but be careful of a couple of things...
(1) When adding Razor support, this is done in the Views/Web.config file not the web.config file in the root of your project.
(2) If you do happen to add a Razor file (.chtml or .vbhtml) OUTSIDE of the Views directory, vs.net will update your root web.config with the following value
<appSettings>
<add key="webpages:Enabled" value="true" />
</appSettings>
Not good. This setting is to allow direct browsing of razor pages and when set to TRUE in my case, caused everything to break. That being said, I'm only using razor pages in my Views subfolder, however what I found nice is making AJAX calls from my .aspx pages to a controller defined in the App_Code directory allowing me to modernize an app that was mostly all postbacks, and C# to access the VB.NET written data layer.

ASP.NET MVC does not support web site template, it can only be Web Application. So you can not add MVC functionality to Web Forms project.

Related

How to convert static web site to ASP.NET Web Pages ("Razor") site?

I'd like to convert my static website to ASP.NET Web Pages ("Razor") site in Visual Studio 2013. The obvious step is to rename my *.html files to *.cshtml and add the dynamic logic. However, I've got a few more questions:
Is web.config required or can I get away without it? If it's required, what's the minimal contents of it?
Do I need to install Web Pages NuGet package? Visual Studio supposedly has Web Pages "included" but I'm not sure what that means for deployment (which will be Azure Web Sites).
I'm asking because I've tried to install the NuGet package but when I run the site, it says "Could not determine which version of ASP.NET Web Pages to use" even though the NuGet package had every chance to update the web.config appropriately. So maybe there's more to the conversion process but I couldn't find it documented anywhere.
Alright, I'm not sure which version of ASP.NET Web Pages you are using, but in your website there should be a Web.config file at the root. In here add the following config.
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>
In the event that you already find a <appSettings> section please only include <add key="webpages:Version" value="2.0.0.0" /> within the existing <appSettings> section.
This should fix the problem. If you do not know which version of the ASP.NET Web Pages assemblies you are using, you can check the Packages.config file that was added when you used nuget to add ASP.NET Web Pages to your website.
Hope this helps!
----OLDER ANSWER: NOT RELEVANT TO OP----
What is your familiarity level with ASP.NET MVC? The proper terminology would be How to convert my static web site to a ASP.NET MVC Website. There are multiple versions of the ASP.NET MVC framework with version 5 being the latest. Unlike earlier Microsoft Web Forms framework, ASP.NET MVC relies more on convention over configuration.
MVC heavily relies on the common agreement of a convention such as folders being structured in a particular format. For example,
Controller classes must be named with a suffix of “controller” such
as MembershipController or AccountController.
Controller classes should be placed in a folder called Controllers.
Views and partial views should be placed in a folder following the convention of
“Views/ControllerName” e.g. "Views/Membership".
Your page layouts should be prefixed with an underscore “_” character and layout
files are placed in the “Views/Shared” folder
Learning these conventions would give you a better understanding of what it takes to convert your website to a ASP.NET MVC website and identify whether that is the approach you want to take.
As far as your questions go.
Yes you need to have a Web.config file. This file contains the Main configuration information for your website. When you create a new ASP.NET MVC project in Visual Studio, the project template usually creates a default Web.config file for you.
As far as installing the nuget package is concerned, when you enable your web application for deployment to Azure in Visual Studio, the process sets the Copy Local property to true for any assemblies that are required for MVC and Silverlight Business Applications. This adds these assemblies to the service package that is used for deployment to Azure. More information on deploying a ASP.NET MVC website to Azure from Visual Studio is available here on MSDN
Also I would suggest that you spend 15-20 minutes reading through the article at w3schools to help you get a better understanding of ASP.NET MVC Framework. ASP.NET MVC Internet Application Hope this helps.

Convert a webforms project to a hybrid MVC4 project

At the moment, we have an old style ASP.NET project that has around 800 .aspx pages.
We are looking at migrating this from ASP.NET webforms to MVC4 + Razor over time, but do not want to have to scrap the entire project and start again.
Is it possible to convert the project file to an MVC project and add the required files/folders for the project to run as an MVC project, but at the same time, preserving the original file/folder structure allowing the existing/legacy pages to still be accessed in the same way as they are now accessed.
You may want to check below links:
http://rachelappel.com/integrating-aspnet-web-forms-and-aspnetmvc
http://www.hanselman.com/blog/integratingaspnetmvc3intoexistingupgradedaspnet4webformsapplications.aspx
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
Is it possible to convert the project file to an MVC project and add
the required files/folders for the project to run as an MVC project
Based on my experience with a Web Application project on VS2012 featuring several hundred .aspx, it is definitely possible to have webforms and mvc 4 with razor running in the same application.
We did not modify the project file (more on that later). We started from a bare MVC4 application and copied the needed features in our webforms application. This included :
packages and references
directives in the web.config (as far as I remember : extensionless handlers, system.web.webPages.razor element, and some assembly bindings )
most of the global.asax mechanisms ( namely routing for a bare application)
directory structure (models, views and controllers)
This was a tedious work, but it also helped us understand what makes an MVC project work. We still miss some IDE features like not having MVC components (views, controllers) offered when right-click -> Add... I guess this would require a csproj editing. We did not go that far.
To prevent conflicting urls, MVC is used through several dedicated areas which would, in the end, replace the corresponding webforms folders.

Tool or Standard Procedure to add a different type of Visual Studio Project to an existing one?

I have an existing website coded in ASP.Net 4.0 Webforms, and would like to add an "ASP.Net Dynamic Data Entities Web Application" (project templates of VS2010 to my actual project):
So my question is, what are the steps to "merge" the 2 projects or integrate the "ASP.Net Dynamic Data Entities Web Application" to my actual webforms.
I think I must merge my Global.asax.cs files and web.config file + move rest of the files. Is that correct ?
I already read that post (that may be outdated?):
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
Thanks for your answers :)
(the original title of this post was: “ASP.Net Dynamic Data Entities Web Application” integration with an existing Webforms)
I've read Scott Hanselman's article which you've provided in your question and yes the same technique still applies today, here are a few steps/considerations which should help you along the way:
When you create the new ASP.NET Dynamic Data Entities Web Application make sure that it's the same .NET Framework version as your Web Forms project, to avoid version conflicts.
Remember to add a reference to all the required libraries that you've been using in the Web Forms project.
Now it's safe to copy all the necessary .aspx pages to the new site.
There is some automatically generated code inside the Global.asax of a Dynamic Data project which is used to configure ASP.NET routing, it may be useful later so instead of replacing it with your Global.asax use a tool like WinMerge to merge the two files.

Reference aspx pages from different web project

I have an ASP .NET web form solution with multiple web projects. There is one core web project and each of the other web project work as pluggable modules.
I have written a post build event to copy aspx/ascx files and *.dll of child web projects to the core web project directory.
The issue is - I am not able to call child project aspx pages from the core project. I get "Could not load type" error.
Is it possible to use aspx pages in different project if i refer that webproject dll and copy the aspx files to the main application? What am i doing wrong here. I would greatly appreciate any pointer in the right direction. Thank you.
Sanjay
I found answer to this. The key is to define you assemblies in the web.config file and then define a probing path. Following is the link with details-
http://weblogs.asp.net/chrismoseley/archive/2008/10/28/shared-assemblies-without-the-gac.aspx

MVC 3.0 Razor Assembly Directive

What is the syntax/analogue for Assembly Directive http://msdn.microsoft.com/en-us/library/a7c375wt(VS.71).aspx in MVC 3.0 Razor
On MVC View i have this:
<%# Assembly Name="Web.Plugins.Authentication" %>
How i may say the same in MVC 3.0 razor view ?
More details and solution:
I did build pluggable MVC application where i am having one Main App and a lot of Plugins in it. All assemblies and views output from Plugins Apps located not in Bin directory of Main App, and in Razor case i was experienced some problems that views cant find model classes.
Finally i did come the solution for that problem. I did make output of all *.dll of Plugins Apps in to Bin Directory of "Main App".
You will not find the equivalent of the Assembly Directive in Razor.
The reason is somewhat convoluted, but it begins with the decision of the MVC team to use WebForms for the first two versions of ASP.NET MVC. WebForms are used for many more things than just views in MVC. What MVC does is simply to re-purpose the WebForms engine to render views.
On the other hand, Razor is a plain view engine. Its objective is to provide a language to describe the rendering of HTML in an MVC website, nothing more.
Perhaps if you detail what you want to achieve we could help you in more detail
Yet another.
web.config
system.web
compilation
assemblies
add assembly
MVC RazorView class(inherit BuildManagerCompliledView) is using BuildManagerWrapper.
If code base change ,implement IBuildManager.GetReferencedAssemblies method and custom WebPage Helper.
In that case all *.dll's should be located in the correct place - in Bin directory of the main project "Main App".

Resources