http://www.aspnetmvcninja.com/general/mixing-asp-net-mvc-and-webforms

Do you have a existing ASP.NET Web Forms project that you would love to migrate to ASP.NET MVC but can’t because you have so much invested in it? I know I did and I’m sure many of you do. The good news is that ASP.NET MVC and ASP.NET Web Forms are compatible allowing you to mix ASP.NET MVC and ASP.NET Webforms in the same project. In this ASP.NET MVC tutorial I’ll show you how to add support for ASP.NET MVC to an existing ASP.NET Web Forms project.

The advantage of adding ASP.NET MVC to an existing ASP.NET Web Forms application is that you can keep your existing code and write all new code using ASP.NET MVC.

So how do you do it? First you need to add references to the following assemblies:

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

Once you have added those you need the special ASP.NET MVC folders and you probably want a couple of files that an empty ASP.NET MVC application gets by default. The easiest way to do this is to create an empty ASP.NET MVC project then copy the following folders with their contents and sub folders into your existing ASP.NET Web Forms project

  • Content
  • Controllers
  • Models
  • Scripts
  • Views

This gives you all of the special folders and files that you need.

You then need to either create a new Global.asax file or modify your existing one. If you are editing an existing one then you need to add the following to your Global class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

All of that is from the standard Global.asax file for a new ASP.NET MVC 3 application except for the following line which stops routing of any .aspx URL.

1
routes.Ignore("{resource}.aspx/{*pathinfo}");

You also need to add the following code to your Application_Start() method in Global.

1
2
3
4
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

Finally you need to make some changes to your Web.config. As with the previous changes it’s best to copy the missing settings from an empty ASP.NET MVC application. In my case I had to add the following to support ASP.NET MVC 3 with the ASPX view engine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<appSettings>
  <add key="ClientValidationEnabled" value="true"/>
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.0">
    <assemblies>
      <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
  </compilation>
  <authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2880" />
  </authentication>
  <pages>
    <namespaces>
      <add namespace="System.Web.Helpers" />
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Routing" />
      <add namespace="System.Web.WebPages"/>
    </namespaces>
  </pages>
</system.web>
<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

You can now create a test controller to make sure that you’ve got everything setup correctly. I used the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Example.Controllers
{
    public class HelloController : Controller
    {
        public string Index()
        {
            return "Hello World!!";
        }
    }
}

Now run your application and go to /hello. If it works you should see the response Hello World!!

UPDATE 2011-02-28:
After making these changes Visual Studio still didn’t treat the ASP.NET Web Forms project as a ASP.NET MVC project. To get Visual Studio 2010 to treat the ASP.NET Web Forms project like an ASP.NET MVC project I needed to add a GUID to ProjectTypeGuids in my projects .web file. The exact GUID depends on the version of ASP.NET MVC you are using.

ASP.NET MVC 3 – {E53F8FEA-EAE0-44A6-8774-FFD645390401}
ASP.NET MVC 2 – {F85E285D-A4E0-4152-9332-AB1D724D3325}

For ASP.NET MVC 3 my file has the line:

1
<ProjectTypeGuids>{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>;

Once that was done Visual Studio behaved properly and displayed all of the ASP.NET MVC specific items in its context menus.

PS: If anyone has the GUID for ASP.NET MVC 1 then please send it to me and I’ll add it above.

转载于:https://www.cnblogs.com/realize/archive/2012/02/23/2364876.html

Mixing ASP.NET MVC and Webforms相关推荐

  1. Asp.net MVC 教程汇总

     自学MVC看这里--全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想 ...

  2. ASP.NET MVC 教程学习

    1. Why :为什么需要ASP.NET MVC 本章主要为大家汇总了为什么学习Asp.net MVC替代WebForms,产生ASP.NET MVC 的需求是什么,只有更好的理解了为什么需要MVC, ...

  3. ASP.NET vs MVC vs WebForms

    许多ASP.NET开发人员开始接触MVC认为MVC与ASP.NET完全没有关系,是一个全新的Web开发,事实上ASP.NET是创建WEB应用的框架而MVC是能够用更好的方法来组织并管理代码的一种更高级 ...

  4. 一起谈.NET技术,专访微软MVP衣明志:走进ASP.NET MVC 2框架开发

    日前微软已经发布ASP.NET MVC 2框架RC版,究竟这次RC版本的发布对于WEB开发者带来怎样的改变?以及未来ASP.NET MVC 2正式版还会有哪些改进?带着这样的问题,我们51CTO记者彭 ...

  5. 如何在FineUIMvc(ASP.NET MVC)视图中绑定多个模型?

    起因 这是知识星球内的一个网友提出的,按理说ASP.NET MVC中一个视图只能绑定一个模型(Model),在视图顶部标识如下: @model IEnumerable<FineUICore.Ex ...

  6. apache2.4.9 开启path_info访问_【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)...

    新建项目 打开VS2015,找到菜单项[文件->新建->项目],打开向导对话框: 注意我们的选择项: 运行平台:.NET FrameWork 4.5 项目模板:ASP.NET Web Ap ...

  7. [转自scott]ASP.NET MVC框架 (第二部分): URL路径选择

    英文原文地址:http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.as ...

  8. asp.net mvc 页面传值的方法总结

    转自:http://msprogrammer.serviciipeweb.ro/2012/01/15/usual-methods-to-transfer-data-from-page-to-page- ...

  9. 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC

    七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)-- ...

最新文章

  1. android中static方法,StaticLayout如何在Android中使用?
  2. python快速编程入门课后题答案-《Python编程:从入门到实践》第五章 if语句 习题答案...
  3. 树莓派安装python模块_树莓派引脚编号、pypi说明和安装
  4. HTML 怎么修改,怎么修改HTML
  5. cidaemon.exe是什么进程及如何关闭cidaemon.exe进程
  6. 全面讲解Python列表数组(二),列表分区/片,列表操作符,比较操作符,逻辑操作符,连接操作符,重复操作符,成员关系操作符;
  7. how to rank conferences or journals?
  8. 解决:object_detection/protos/*.proto: Invalid argument.
  9. 计算机视觉算法实战书籍推荐_岗位内推 | 字节跳动招聘NLP、计算机视觉、推荐算法实习生...
  10. feign和ajax,SpringCloud-feign 声明式服务调用
  11. mysql sql 检测磁盘_MySQL 数据库磁盘占用情况查询
  12. mysql插入另一个表中数据_MySql中把一个表的数据插入到另一个表中的实现
  13. 双系统ubuntu 删除后重装
  14. 干货 | 科研大牛们怎么读文献?
  15. ML/DL-复习笔记【九】- 神经网络中各层的计算量与参数量
  16. scratch动态三角形拖动/自制素材/少儿编程scratch教研教案课件课程素材脚本
  17. 基于Radon滤波反投影算法的CT图像重建matlab仿真
  18. 如何在UltraCompare中编辑文件?
  19. python 趣味编程课_Python趣味编程公益课开班,期待你的到来~
  20. 旅行商问题与蚁群算法

热门文章

  1. Css标题中图片居中,图片居中:任意图片在div里的上下垂直都居中!
  2. windows服务器修改端口号,windows服务器修改远程连接端口图解
  3. java里的字符流_javaIO流中字符流的应用
  4. python如何计算分子描述符_Python——描述符(descriptor)解密
  5. 进程间通信--信号(SIG)
  6. atexit注册进程终止处理函数
  7. golang基于UDP完成
  8. 整数反转—leetcode7
  9. linux基础——linux进程间通信(IPC)机制总结
  10. kali 安装搜狗输入法