c# MVC 在工程中應用的實際處理

1.把collection放到另外一個專案中。

  添加一類別庫 ClassLibrary1,在此類別庫中,定義所有的collection

2.在webmvc的項目中引用此類別庫。

   為了正常使用,需要在此項目中的Global.asax文件中,做如下定義。

  

      public static void RegisterRoutes(RouteCollection routes)
{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
, new string[] { "ClassLibrary1" }
);

}

在這裡要寫上collection所在的命名空間。

3.為了使用一個不同的文件夾放頁面文件,需要下面幾步

  3.1 建立文件夾 ViewTest,然後定義和默認view文件夾一樣的目錄。如果不希望建立shard文件夾,可以使用view下的。

  3.2 添加一個Web.config文件。和默認的view一樣的文件。否則顯示不正常。

  3.3 添加_ViewStart.cshtml文件。和默認的view一樣的文件。

  3.4 在我這個測試項目中,是混用的。

  3.5 然後在Global.asax文件中,做如下定義:

     public static void RegisterRoutes(RouteCollection routes)
{
RazorViewEngine razor=new RazorViewEngine();
razor.ViewLocationFormats = new string[] {
"~/ViewTest/{1}/{0}.cshtml",
"~/ViewTest/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml" };

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(razor);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
, new string[] { "ClassLibrary1" }
);

}

   對應上面的代碼的說明:

   定義一個RazorViewEngine,然後對ViewLocationFormats 進行重新定義。告訴view都去什麽地方查找。

   很簡單吧。


posted @ 2012-03-18 17:26  無限遐想  阅读(1938)  评论(39编辑  收藏  举报