WebForm与MVC混用
MVC WebForm
2023-09-11 14:21:57 时间
WebForm与MVC混用
步骤一:添加引用 -> 程序集 -> 扩展 ->
- System.Web.Mvc ;
- System.Web.Razor;
- System.Web.WebPages;
- System.Web.Abstractions;
步骤二:mvc默认目录结构
新建一个空的mvc项目, 将Controllers和Views文件夹全部拷贝过来(文件结构和web.config这个很重要)
步骤三:修改配置文件web.config(不是Views文件夹下的)
1.在compilation节点下加入以下几个assemblies :
<compilation debug="true" targetFramework="xx"> // 框架版本不用动 <assemblies> <add assembly="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </assemblies> </compilation>
2. 还有一个需要注意的: 在configuration节点下加入 <modules runAllManagedModulesForAllRequests="true"/>
-- 不过我把这个去掉也调试成功了, 这个貌似只是设置每个请求都会执行Global.cs里边的BeginRequest事件而已
<configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer>
步骤四:路由设置
1. 可以按照mvc惯例 在App_Start文件夹中创建路由配置静态方法;RouteConfig.cs文件代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; // 引入命名空间 才能注册mvc的路由 using System.Web.Routing; // 引入命名空间 namespace webform { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); // web form访问方式 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } }
2. 然后在Global.cs中注册
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes);
接着就可以写你的controller和View
按照以上步骤调试成功 ╮(╯▽╰)╭
ps: 个人对配置文件里的各个节点作用不是很理解, 就新建了一个空mvc项目然后把Views里边的东西全部拷贝过来
相关文章
- ASP.NET Core MVC/WebAPi 模型绑定探索
- MVC之Ajax.BeginForm使用详解之更新列表 mvc验证jquery.unobtrusive-ajax
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
- asp mvc @Html.CheckBox("sel",true) 往后台传值问题
- Spring mvc
- MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证
- .NET-MVC添加属性验证
- 《Spring MVC学习指南(第2版)》——1.3 小结
- 基于Spring+Spring MVC+Mybatis的B2C购物网站
- 使用Mono.Cecil辅助ASP.NET MVC使用dynamic类型Model