如何使用asp.net 4.0+webform程序中集成mvc4
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章给大家介绍如何使用asp.net 4.0+webform程序中集成mvc4,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1、新建packages.config文件,里面加
千家信息网最后更新 2026年11月01日如何使用asp.net 4.0+webform程序中集成mvc4
这篇文章给大家介绍如何使用asp.net 4.0+webform程序中集成mvc4,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1、新建packages.config文件,里面加上必要的程序集
2、在对应web项目中还原包
update-package -projectname 'web' -reinstall
3、新建App_Start目录,在里面加上mvc对应配置代码

BundleConfig.cs为静态文件压缩的配置代码,参考代码如下:
public class BundleConfig { // 有关 Bundling 的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=254725 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js")); bundles.Add(new ScriptBundle("~/bundles/common").Include("~/js/common*")); bundles.Add(new ScriptBundle("~/bundles/echarts").Include("~/js/echarts.common*")); bundles.Add(new ScriptBundle("~/bundles/mustache").Include("~/js/mustache*")); bundles.Add(new ScriptBundle("~/bundles/blockUI").Include("~/js/jquery.blockUI*")); bundles.Add(new StyleBundle("~/Content/oa/css").Include("~/css/oa/style.css")); //BundleTable.EnableOptimizations = true; } }RouteConfig.cs为路由配置代码,web form相关资源要在此处忽略路由过滤
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); //routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); routes.IgnoreRoute("{handler}.ashx/{*pathInfo}"); routes.IgnoreRoute("Handlers/{handler}.aspx/{*pathInfo}"); routes.IgnoreRoute("ajaxpro/prototype.ashx"); routes.IgnoreRoute("ajaxpro/core.ashx"); routes.IgnoreRoute("ajaxpro/converter.ashx"); routes.IgnoreRoute("ajaxpro/{resource}.ashx"); routes.IgnoreRoute("{resource}.asmx/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }WebApiConfig.cs为WebApi的路由配置,参考代码:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }4、Global文件中Application_Start事件中加上如下代码,使程序启动MVC配置生效
AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json")); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();5、新建Controllers文件夹,在里面加上控制器类,例如
public class DocRecController : Controller { public ActionResult Index() { ViewBag.UserName = "wilson.fu";return View(); }}6、新建Views文件夹,里面加上对应视图文件,如果需要使用模板,还需增加_ViewStart.cshtml 文件,例如DocRec/Index.cshtml,文件如下
@{ Layout = null;} @ViewBag.UserName
Views文件夹下还需要加上Web.config文件进行请求过滤
目录结构如下:

编译通过后,访问/docrec/index,即可看到效果:
关于如何使用asp.net 4.0+webform程序中集成mvc4就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
文件
代码
配置
程序
文件夹
路由
参考
中集
内容
更多
目录
帮助
不错
必要
事件
信息
兴趣
小伙
小伙伴
控制器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技网
江苏根荣互联网科技
重生之互联网科技帝国
互联网科技公司税收优惠
互联网为科技
智网互联网科技
互联网科技logo设计
天下捷融互联网科技
互联网养老 桔豆科技
四个字互联网科技公司取名
农村开互联网科技讲堂
互联网科技博主是什么意思
央行互联网金融科技公司
宁夏众和广鑫互联网科技有限公司
互联网科技革命什么开始
互联网金融科技展
北京天晋互联网科技有限公司
唐山双旺合互联网科技产业园
黑龙江互联网科技公司
互联网科技自媒体灵感
咖啡网
深圳市人人互联网科技有限公司
厦门中拓互联网络科技有限公司
互联网是不是新科技
广州互联网科技公司
互联网科技公司官网
科技与互联网的利与弊
重庆互联网医院沙龙医盟信息科技
互联网时代科技型公司的机会
科技美学互联网 艺术