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

來源:互聯網
上載者:User

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都去什麽地方尋找。

   很簡單吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.