Advantage of create ASP.NET MVC project with WebForm core reference - asp.net-mvc

The 'New ASP.NET Project' dialog lets you create an MVC Project, and allows you to add a 'core reference' to Web Forms.
What is the advantage and usage of adding the WebForms reference?
Is it so the web project can contain both MVC views and controllers as well as WebForms pages?

Is it so the web project can contain both MVC views and controllers as well as WebForms pages?
Indeed. And if you check Web API, you can even create ApiController-derived controllers.
Sometimes you want to use multiple techniques in a single project. If you don't need it: don't check it. You can always add it manually when required later.

Related

Create separated MVC5 projects into same solution

I'm new to MVC. I created a ASP.NET MVC5 Login project (UI) using Razor engine that is authentication responsible by using OWIN (VS2013). ViewModels and Data for solution are separated and layered by DLL. I would like to create two separated MVC5 project (UI) into same solution, each one is like a subsystem (eg: Accounting project, Inventory project, etc). Always user has to pass authentication project (Login) first and then according its necessity navigate to Accounting or Inventory subsystem.
My goal is that I could add or remove subsystems when deploying according company negotiation that let it use one or any subsystem.
How can I achieve that?
Every subsystem must be created using a separated ASP.NET MVC5 project template or can be created by a separated DLL?
You can use areas. See http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm
What are Areas?
Areas are functionally independent modules of your ASP.NET MVC application that mimic the folder structure and conventions of MVC. Consider the following scenario :

Can MVC framework be used in a web application that is currently using ASP .NET?

I recently joined a group that manages a Classic ASP web application. It has been working fine for our group's need. However, a decision was made, before I joined, to move to ASP .Net. Since we are mostly ASP developers, we write code in ASP .Net as we would in Classic ASP (for the most part). Would it be possible to introduce MVC to this application/project?
Thanks!
Yes, it is possible to use MVC in a traditional WebForms project. I migrated a large WebForms project to MVC 2 a couple of years ago, and here are my findings (I have updated them to reflect MVC 3)
Make sure you have .NET 4.0 installed, as well as the MVC 3 framework and VS extensions.
Create a new blank MVC project to use as a reference.
Look at the default web.config for the reference project. You basically want to use the reference web.config, and merge in stuff you need from your current project.
Look at the reference global.asax.cs. Similar to the above, you want to merge the changes in the reference .cs into your current application's global.asax.cs.
You will need to add the following references to your web project:
System.Web.Abstractions,
System.Web.Extensions,
System.Web.Helpers,
System.Web.Mvc,
System.Web.Routing
You can enable the VS extensions by changing the ProjectTypeGuids:
In Solution Explorer, right-click the project name and select Unload Project. Then right-click the project name again and select Edit ProjectName.csproj.
Locate the ProjectTypeGuids element and add {E53F8FEA-EAE0-44A6-8774-FFD645390401}.
Save the changes, right-click the project, and then select Reload Project.
Add the following standard folders for MVC content:
~/Views
~/Views/Shared
~/Controllers
~/Models (for your view models, optional)
~/Content (for CSS and images, optional)
~/Scripts (for JS, optional)
Additional notes:
If your existing WebForms relies on web.config settings for authorization (such as preventing unauthorized users), this won't be recognized by MVC actions, because routing works completely separately from the WebForms authorization. Use AuthorizeAttribute to require authorization, or constrain actions to certain roles or users. You can even specify global filters so you don't have to apply this attribute on every single controller or action.
There may be additional considerations for making MVC work with IIS versions prior to 7, or with application pools that use the Classic pipeline. Consider using IIS 7+ with Integrated pipeline.
My notes above mainly involve getting the baseline of MVC working, which uses ASPX views. ASPX views use the same markup as the ASPX files you are used to in WebForms. You can also use the new Razor syntax (primer), which I highly recommend. You can use both ASPX and Razor view pages at the same time. However, you cannot use an ASPX master page on a Razor view (or vice versa). Also, MVC will find and use ASPX views before Razor views, so if you upgrade a view to Razor, delete the original ASPX. You will need to do a little additional work to enable Razor views. I'm trying to find my notes for enabling Razor. I'll update when I find them. Once you do have Razor installed and working, you can use this tool by Telerik to convert ASPX to Razor.
Here is a question on SO about a problem I'd had while upgrading. I'm only providing it because it covered some of the points I mentioned above in more detail. However, I was upgrading to MVC 2 at the time, so some of this stuff is out of date.
The short answer is yes it is possible.
Scott Hanselman has written about this topic before.
Depending on the skills in your team, you may well find it difficult to get up to speed - it is possible to write clean MVC style code in Classic ASP but most people don't.
It is of course technically possible. However, it sounds like it would be a cultural shock, as MVC works quite a bit differently than Classic ASP or ASP.NET WebForms. I think it would be worthwhile to do - or just stick with ASP.NET WebForms if that seems more natural. But if you've not yet fully committed to WebForms, MVC seems just as easy to move to, IMO.
Yes. I worked on a project that began as plain ASP.NET and later added some ASP.NET MVC pages. Eventually we liked MVC so much more that we eventually migrated all of our WebForms pages to use MVC instead. But the whole time, the two systems worked very well together.
Here's the only real gotcha that I can remember running into: WebForms works by having the entire page encased in a big <form> tag. Since HTML doesn't allow you to have nested <form> tags, you typically can't use MVC forms inside of a WebForms page. Either keep your WebForms pages separate from your MVC content, avoid using HTML forms in MVC content that may appear on a WebForms page, or use popup dialogs for your forms that get created outside of the WebForms DOM area.

Add controller/view on other folders

Initial sidenote: I'm working on a hybrid WebForms+MVC application that started as a WebForms application hence I've added project GUIDs that converted it to MVC. This means that I'm getting context menus directly on Controllers and Views folder in application root. That works.
I was wondering whether it's possible to convince Visual Studio (and Asp.net MVC tooling) to add Add controller... and Add View... context menu items to other folders not just those default ones that are on the project root?
The reason is that I'm working on an Asp.net WebForms application that is now a hybrid with MVC. And since it already has quite a few folders in root I would rather separate the new MVC part and contain it altogether within mvc folder, so all MVC related files would be inside that particular folder.
But right clicking on mvc\Controllers doesn't give me the wizard, nor does the mvc\Views... Also navigation from controller code to views isn't working...
Is there any way to customize this in Visual Studio? Do Asp.net MVC tools for VS have some sort of configuration file where this can be configured? Or in registry maybe?
If you wish to separate the MVC stuff from the rest of the application you might want to put it in a separate Area.
You'd have the context menu and separation from the existing mess.

Side-by-Side Asp.Net and MVC Razor

We have an existing ASP.Net Web Application. I would like to create an ASP.Net MVC Razor Application where the two applications will work together. A single Master Page would contain menu items that can call .aspx pages as well as Razor .cshtml pages.
I have seen an example using MvcContrib Portable areas utilizing Routing. This particular example has .aspx pages in both (the MVC was not Razor).
Is there an example out there that will show the two running side-by-side and the MVC is Razor? It would be best if I could download a visual Studio Solution so that I can run this.
I am not sure if the MvcContrib way is the latest and best way to achieve this.
I do not want to go Hybrid!
You don't need any other external librarry. You can always convert the existing ASP.NET web forms Project to be a Hybrid one which uses webforms and MVC. You need to add the required MVC assembly references and make some changes to the web.config and you are all set. Scott has a simple and awesome blog post about this where he explains how to do the conversion.
I scribbled a note about how to enable the MVC specific Context menu( Add Controller / Add View) in the hybrid project after conversion here

Can i add MVC project in normal asp.net c# project?

can anybody tell me that if i made a normal asp.net c# project and in that solution i want to add mvc project. so is it possible to call mvc view from normal page which is in first project? if yes then how can i call view page from normal page of asp.net c# project.
on live server how can i call mvc page?
Thanks
Samir
Mixing VB ASP.NET and C# ASP.MVC projects in a single solution
Ok, couple of different things here.
Can a ASP.NET forms project exist in the same solution? Yes
Can you mix MVC with WebForms in the same project? No
Can a MVC site communicate with a WebForms site? Yes, you can easily pass between via simulated form submission or by URL. You could also configure cookies to work across both sites.
Can you have an MVC site and a WebForms site use the same url? No

Resources