\t\tASP.NET MVC 2中的Area特性

没有Areas前相同问题的处理

在mvc1.0时代,如果要将网站按目录结构来区分。例如

  
  
  1. Website/Index
  2. Admin/ Index
  3. User/ Index
  4. ……/……

通常都是在Views下面建立若干个和Controller相对应的目录,然后在里面放置aspx页面

  
  
  1. Views\Website\Index
  2. Views\Admin\Index
  3. Views\User\Index
  4. Views\.......\.......

这样建立若干个目录

其实这样也没什么不好,唯一不好的可能就是随着业务的需要,结构需求会越来越多,views目录下面的文件夹越来越多,更或者你需要更细结构的页面路径,例如:

  
  
  1. Website/Product/Index
  2. Website/Catalog/Index
  3. Website/Contect/Index

当然,你可以用UrlRouteing或者ViewEngine搞定这些问题。但是毫无疑问,随着网站的运行日久,同一个Controller目录下的文件会越来越多,对于同一个Controller下的ActionResult的命名和UrlRouting里面的维护带来不小的麻烦。给管理带来不方便【个人理解】。

现在出Areas之后,这个问题有所缓解。还是如上的Url

  
  
  1. Website\Product\Index
  2. Website\Catalog\Index
  3. Website\Order\Index
  4. Website\Contact\Index

可以使用mvc2.0新增的Area来解决这个问题

建立项目

首先,用mvc2建立一个新项目,在网站根目录下建立Areas文件夹,在Areas文件夹建立你要区分的目录,例如本例的Website,然后继续在Website目录下增加Views目录,继续在views目录下增加需要分类管理Controller目录和建立aspx文件。使文件结构形成

  
  
  1. Areas\Website\Views\Product
  2. Areas\Website\Views\ Catalog
  3. Areas\Website\Views\ Order
  4. Areas\Website\Views\ Contact

建立项目

到原有默认的views目录将web.config复制到现在的新的views目录,你甚至现在可以把原有的views目录删除掉

建立Areas区域UrlRouting

随便找个地方,建立一个新的类,继承AreaRegistration实现抽象类

修改Global.sas

  
  
  1. protected void Application_Start()
  2.          {
  3. AreaRegistration.RegisterAllAreas();
  4. //注册区域Url规则,注意先后顺序
  5. RegisterRoutes(RouteTable.Routes);
  6.          }
为区域页面建立Controller类

为区域页面建立Controller类没什么区别,可以建立在另外一个外部类库项目上,唯一需要注意的就是命名空间需要和注册Area规则的类的命名空间的前导一致。我们知道,在不使用Areas的时候Controller是不受namespace约束的。也就是说只要你有一个Controller名,而不管他在哪个命名空间下都是可以起作用的,如果我们在不同的命名空间建立2个相同的Controller类名,编译的时候不会出错,但是运行mvc网站的时候会提示存在2个相同的Controller类,系统不知道使用哪个。但是Areas却有所限制,他一定要命名空间的前导和AreaRegistration类得命名空间相同。例如:我建立的AreaRegistration网站项目命名空间为Valor.Asmyna.Areas.Website然后我将Controller分开作为一个独立的类库,如果我随便写一个命名空间空间,这个Controller对于Area里面的views是不起作用的,但是他却对原始Views目录的Controller起作用,只有将他的命名空间设置成Valor.Asmyna.Areas.Website.xxx.xxx的前导才起作用

  
  
  1. namespace Valor.Asmyna.Areas.Website
  2. {
  3.     public class HomeController : Controller
  4.      {
  5. public ActionResult Index()
  6.          {
  7.              ViewData["title"] = "Website/Home/Index";
  8.             return View();
  9.          }
  10.      }
  11. public class ProductController : Controller
  12.      {
  13.         public ActionResult Index()
  14.          {
  15. ViewData["title"] = "Website/Product/Index";
  16. return View();        }
  17.      }
  18. public class ContentController : Controller
  19.      {
  20.         public ActionResult Index()
  21.          {
  22.    ViewData["title"] = "Website/Content/Index";
  23.     return View();
  24.          }    }
  25. }

Ok,到浏览器测试一下看看

Area结构完全一致会出现的问题

我们继续在Area目录下增加一个Home目录,在他的Veiws目录下也增加三个相同的controller目录

浏览器中测试

直接在刚才注册Website AreaRegistration命名空间为他注册一个Area规则,用默认系默认的Controller为Home.,

对2个路径进行访问:

/Website/Product

/Home/Product

这个时候controller对于这2个area目录的views都能起作用。在页面打印得到的结果一致

View结果

显然这样是不对的.由此我们刚才想到Area的Controller的选择名命名空间限制问题。那我们他们分开来注册看看。修改Home区域的AreaRegistration的命名空间和在为HomeArea建立一个Controller类,使他们的命名空间一致。这次我们用Valor.Asmyna.Areas.Website

  
  
  1. namespace Valor.Asmyna.Areas.Home{
  2.    public class HomeController : Controller
  3.      {
  4.        public ActionResult Index()
  5.          {
  6.              ViewData["title"] = "Home/Content/Index";
  7.             return View();
  8.          }    }
  9. public class ProductController : Controller
  10.      {
  11.         public ActionResult Index()
  12.          {
  13.              ViewData["title"] = "Home/Content/Index";
  14.             return View();
  15.          }}
  16.     public class ContentController : Controller
  17.      {
  18.         public ActionResult Index()
  19.          {
  20.              ViewData["title"] = "Home/Content/Index";
  21.             return View();
  22.          }
  23.      }}
  24. namespace Valor.Asmyna.Areas.Home
  25. {
  26.     public class HomeController : Controller
  27.      {
  28.         public ActionResult Index()
  29.          {
  30.              ViewData["title"] = "Home/Home/Index";
  31.             return View();
  32.          }    }
  33. public class ProductController : Controller{
  34.        public ActionResult Index()
  35.          {
  36.              ViewData["title"] = "Home/Product/Index";
  37.             return View();
  38.          }     }
  39.     public class ContentController : Controller
  40.      {
  41.         public ActionResult Index()
  42.          {
  43.              ViewData["title"] = "Home/Content/Index";
  44.             return View();
  45.          }
  46.      }
  47. }

编译之后访问,各自分别为自己的Controller处理了

Home/Product

结果1

Website/Product

结果2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值