webform如何升级mvc

1.创建项目

在这里插入图片描述
给项目起名字然后指定存储位置
在这里插入图片描述
选择asp.net 空项目
在这里插入图片描述

2.新建model

namespace WebFormToMvc
{
    /// <summary>
    /// 用户模型
    /// </summary>
    public class UserModel
    {
        /// <summary>
        /// id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }
    }
}

3.新建Service,模拟数据库调用

using System.Collections.Generic;

namespace WebFormToMvc
{
    public class Services
    {
        /// <summary>
        /// 获取全部的用户
        /// </summary>
        /// <returns></returns>
        public List<UserModel> GetAllUsers()
        {
            //模拟数据库查询
            List<UserModel> res = new List<UserModel>();
            for (int i = 0; i < 10; i++)
            {
                res.Add(new UserModel() { Id = i + 1, Name = "小明" + i });
            }
            return res;
        }
    }
}

4.新建aspx页面

新建index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebFormToMvc.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater runat="server" ID="rpt1">
            <ItemTemplate>
                <div>id:<%#Eval("Id") %></div>
                <div>姓名:<%#Eval("Name") %></div>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

index.aspx.cs

using System;

namespace WebFormToMvc
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.rpt1.DataSource = new Services().GetAllUsers();
            this.rpt1.DataBind();
        }
    }
}

在这里插入图片描述

5.配置mvc

5.1 安装nuget包

Microsoft.AspNet.Mvc
Microsoft.AspNet.Mvc.zh-Hans
Microsoft.AspNet.Web.Optimization
Microsoft.AspNet.Razor
在这里插入图片描述

5.2 新增全局程序类

在这里插入图片描述

5.3 创建App_Start和Controllers以及Views文件夹

App_Start下新建RouteConfig

using System.Web.Mvc;
using System.Web.Routing;

namespace WebFormToMvc.App_Start
{
    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 }
            );
        }
    }
}

Controllers下新建HomeController

using System.Web.Mvc;

namespace WebFormToMvc.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var users = new Services().GetAllUsers();
            return View(users);
        }
    }
}

Views下新建web.config
引入mvc控制器

<?xml version="1.0"?>

<configuration>
	<configSections>
		<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
			<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
			<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.9.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
		<pages pageBaseType="System.Web.Mvc.WebViewPage">
			<namespaces>
				<add namespace="System.Web.Mvc" />
				<add namespace="System.Web.Mvc.Ajax" />
				<add namespace="System.Web.Mvc.Html" />
				<add namespace="System.Web.Optimization"/>
				<add namespace="System.Web.Routing" />
			</namespaces>
		</pages>
	</system.web.webPages.razor>

	<appSettings>
		<add key="webpages:Enabled" value="false" />
	</appSettings>

	<system.webServer>
		<handlers>
			<remove name="BlockViewHandler"/>
			<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
		</handlers>
	</system.webServer>

	<system.web>
		<compilation>
			<assemblies>
				<add assembly="System.Web.Mvc, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
			</assemblies>
		</compilation>
	</system.web>
</configuration>

Views下新建Home/Index.cshtml

@model List<WebFormToMvc.UserModel>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>首页</title>
</head>
<body>
    <div>
        @foreach (var item in Model)
        {
            <div>id:@item.Id</div>
            <div>姓名:@item.Name</div>
        }
    </div>
</body>
</html>

修改Global.ashx

using System;
using System.Web.Mvc;
using System.Web.Routing;
using WebFormToMvc.App_Start;

namespace WebFormToMvc
{
    public class Global : System.Web.HttpApplication
    {
        /// <summary>
        /// 应用程序启动时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            //添加其他过滤器
            //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //添加捆绑
            //BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

在这里插入图片描述
实现了aspx转mvc,也能保证aspx和mvc项目的共存

参考1
参考2
参考3

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值