十步让 WebForm项目 变为 Mvc项目

时间:2020-12-29 13:27:40

1、创建一个项目名为 App_Asp 的 Asp.NET 空 Web 应用程序
2、添加全局应用程序类 Global.asax
3、富文本打开 Global,修改 Inherits 为 App_Asp_Global  ->  App_Asp.MvcApplication
4、Golbal : System.Web.HttpApplication  ->  MvcApplication : System.Web.HttpApplication
5、添加 System.Web.Routing 引用,添加 App_Start 文件夹,添加 RouteConfig.cs 类,修改 命名空间为 App_Asp,添加 System.Web.Mvc 引用(Mvc4),RouteConfig 添加 System.Web.Mvc 引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;

namespace App_Asp
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
Application_Start 注册路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace App_Asp
{
    public class MvcApplication : System.Web.HttpApplication
    {

protected void Application_Start(object sender, EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

6、添加 Controllers 文件夹,添加 HomeController 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App_Asp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return Json(new { isOk = true, msg = "成功" }, JsonRequestBehavior.AllowGet);
        }
    }
}
7、返回值从 JsonResult 换成 ViewResult
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App_Asp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //return Json(new { isOk = true, msg = "成功" }, JsonRequestBehavior.AllowGet);
            return View();
        }
    }
}
添加 Views/Home/Index.cshtml 文件,内容如下
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        这是我的Mvc测试页面
    </div>
</body>
</html>
8、添加 System.Web.WebPages 引用
9、添加 Views/Home/Web.config 文件,内容如下
<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
    </pages>
  </system.web.webPages.razor>
</configuration>
10、分别设置 System.Web.Mvc、System.Web.WebPages 属性 复制本地  -> True