昨天经理给讲了下Areas感觉挺好用的今天拿出来给大家分享下(那里不对的地方多多指教)。
(Areas)可以将Asp.net MVC应用的功能进行分割和组合,它可以将Controller和View进行分组管理。
我们在VS2010里面可以这样建Areas(如下图所示)
建完之后的Areas
它会自动生产 M-V-C我们不要手动生成免得出现不必要的错误。
当你建好运行的时候这时候就会报错了!!
这主要是同名的HomeController所导致的。
解决办法:
/Areas/Admin/AdminAreaRegistration.cs
代码
1
context.MapRoute(
2 " Test_default " ,
3 " Test/{controller}/{action}/{id}/ " ,
4 new { controller = " Home " , action = " Index " , id = UrlParameter.Optional },
5 new string [] { " CSSD.Web.Areas.Admin.Controllers " }//加上这一句
6 );
2 " Test_default " ,
3 " Test/{controller}/{action}/{id}/ " ,
4 new { controller = " Home " , action = " Index " , id = UrlParameter.Optional },
5 new string [] { " CSSD.Web.Areas.Admin.Controllers " }//加上这一句
6 );
/Global.asax里面的代码
代码
1
routes.MapRoute(
2 " Home " , // Route name
3 " {controller}/{action}/{id}/ " , // URL with parameters
4 new { controller = " Home " , action = " Index " , id = UrlParameter.Optional },
5 new string [] { " CSSD.Web.Controllers " }
6 );
2 " Home " , // Route name
3 " {controller}/{action}/{id}/ " , // URL with parameters
4 new { controller = " Home " , action = " Index " , id = UrlParameter.Optional },
5 new string [] { " CSSD.Web.Controllers " }
6 );
这样就解决了添加Areas后出现的错误了。