栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

asp.net 4.0+ webform程序中集成mvc4

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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,即可看到效果:

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号