MVC View與Controller分離

來源:互聯網
上載者:User

標籤:目錄   模板   命名空間   bsp   images   color   out   類庫   register   

建立了一個 Separate 解決方案, 如

 

 

Separate.UI    UI層。 引用 Separate.Home

Separate.Home 把Home控制器分享到 一個類庫中 並引用(System.Web/System.Web.Mvc 程式集)

 

UI層 添加 地區 Home,然後就可以刪除 其地區 Home 的Controllers 目錄了。

複製 地區註冊檔案 到 HomeAreaRegistration.cs並修改其命名空間,並刪除UI層的 地區註冊檔案。

這裡的是

using System.Web.Mvc;namespace Separate.Home{    public class HomeAreaRegistration : AreaRegistration    {        public override string AreaName        {            get            {                return "Home";            }        }        public override void RegisterArea(AreaRegistrationContext context)        {            context.MapRoute(                "Home_default",                "Home/{controller}/{action}/{id}",                new { action = "Index", id = UrlParameter.Optional }            );        }    }}

 

 

視圖放在 UI層的  Areas/地區名/Views/控制器名  下面。

然後訪問

 

 

Separate.Home  建立 AdminController.cs

using System.Web.Mvc;namespace Separate.Home{    public class AdminController : Controller    {        public ActionResult About()        {            return View();        }    }}

 

在UI層    Separate.UI /Areas/Home/ Views/ Admin(此檔案夾要手動建立) 下添加 視圖

@{    ViewBag.Title = "About";}<h2>我是Admin下的About</h2>

 

 

然後訪問

 

 

 

 

 

PS:

Areas/View/Controller 視圖只要放web.config 其視圖,就可以被解析

 

Areas 內的模板 不繼承於 主模板

 

MVC View與Controller分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.