在asp.net-mvc中,如何重構我的site.master頁面以支持2個不同的主視圖?

[英]In asp.net-mvc, how can I refactor my site.master page to support 2 different master views?


I have a team asp.net-mvc website and in the site.master page I have

我有一個團隊asp.net-mvc網站,我在site.master頁面

  • a header image
  • 標題圖片
  • a menu
  • 一份菜單
  • all my js and css includes.
  • 我所有的js和css都包括在內。

I now need to use the site for two different teams where I run the site on two different ports and each team only see's their team's info (pretty much same exact site and same db but just different database filters)

我現在需要將該站點用於兩個不同的團隊,我在兩個不同的端口上運行該站點,每個團隊只看到他們團隊的信息(幾乎完全相同的站點和相同的數據庫,但只是不同的數據庫過濾器)

I achieved the backend by adding this to my controller class:

我通過將其添加到我的控制器類來實現后端:

     string team = ConfigurationManager.AppSettings["Team"]

and based on that value, it changes the DB queries i run. That is working great but now I need to do some tweaking on the frontend (as i realized the header image and some menu items need to change as well)

並基於該值,它更改我運行的數據庫查詢。這工作得很好,但現在我需要在前端做一些調整(因為我意識到標題圖像和一些菜單項也需要改變)

Given that, I now need to refactor the site.master as i need to use the same codebase to support the view for two different teams and I want

鑒於此,我現在需要重構site.master,因為我需要使用相同的代碼庫來支持兩個不同團隊的視圖,我想要

  • Each team to show its own headerImage
  • 每個團隊都展示自己的headerImage
  • Each team to have its own menu items
  • 每個團隊都有自己的菜單項
  • Avoid duplicating any common code (like css an js includes)
  • 避免重復任何常見代碼(如css和js包含)

(which are now just hardcoded in the site.master file)

(現在只是在site.master文件中硬編碼)

I could copy and paste all of the code into a different master page (lets say site2.master) and duplicate all of the common code but I wanted to see if there was the ability to factor out all of the common code between the 2 master files to avoid duplication.

我可以將所有代碼復制並粘貼到不同的母版頁(比如說site2.master)並復制所有常用代碼但是我想看看是否能夠分解2個主代碼之間的所有公共代碼文件,以避免重復。

Also, i am trying to figure out how i can have my code dynamically "figure out" which master file to use based on a config entry. I was thinking about using nested master pages but that doesn't seem to work because each page really is applicable to both Team1 and Team2 and just needs to get set dynamically. I hopefully want to avoid a situation where I have this switch code on EVERY controller action

此外,我試圖弄清楚我如何動態地“弄清楚”基於配置條目使用哪個主文件。我正在考慮使用嵌套的母版頁,但這似乎不起作用,因為每個頁面確實適用於Team1和Team2,只需要動態設置。我希望避免在每個控制器操作上都有這個開關代碼的情況

1 个解决方案

#1


1  

Since you will have different menu items (this is the major part of what you said that is determining my answer), I believe that two master views would be the better approach rather than having either:

由於你將有不同的菜單項(這是你所說的決定我的答案的主要部分),我相信兩個主要觀點將是更好的方法,而不是:

a) a If/Else in your master view or

a)主視圖中的If / Else或

b) a ton of configuration entries that you would have to parse on each request, and

b)您必須在每個請求上解析大量配置條目,以及

c) Provides an easier path for more custom theme-ing in the future if so requested or needed.

c)如果需要或需要,將來可以為更多自定義主題提供更簡單的途徑。

But, there are definitely ways to reduce the amount of code that you need to write and avoid duplication. Below are some suggestions that I would recommend

但是,有一些方法可以減少您需要編寫的代碼量並避免重復。以下是我建議的一些建議

1.Use the same logic as you do with your DB query settings to set the master page depending on the team. Example:

1.使用與數據庫查詢設置相同的邏輯,根據團隊設置母版頁。例:

<appSettings>
   <add key="team" value = "WhateverYouAlreadyHave"/>
   <add key="layout" value = "~/Views/Shared/team.master"/>
</appSettings>

2.In your views, specify your layout view in the top portion as so:

2.在視圖中,在頂部指定布局視圖,如下所示:

@{
    ViewBag.Title = "My Page Title";
    Layout = @ConfigurationManager.AppSettings("layout");
}

3.In your master views, factor our the JS and CSS into a shared partial view that you can call from the master pages

3.在主視圖中,將JS和CSS分解為可以從母版頁調用的共享局部視圖

@{Html.RenderPartial("HeadContent");}

I believe that this achieves your goals of not repeating yourself in your layout information as well as cuts down the potential need of If/Else statements in your master views. Lastly, if you ever had to separate out the sites (different servers or something) you should be able to do that quickly without having dead code floating around your views.

我相信這實現了您不會在布局信息中重復自己以及減少主視圖中If / Else語句的潛在需求的目標。最后,如果您不得不將網站(不同的服務器或其他東西)分開,您應該能夠快速完成,而不會在視圖周圍浮動代碼。

Lastly, I suppose an alternative solution would be to keep a single layout file, but reverse what goes into the shared partial view. Meaning you could store the header image as a configuration value as well as the shared partial view name. In this instance, put your menus in their own partial view that you could call from a single master. However, if you really wanted to provide custom layout/look/theme in the future, this would be more problematic to achieve with a single master page.

最后,我認為另一種解決方案是保留單個布局文件,但反轉共享局部視圖中的內容。這意味着您可以將標題圖像存儲為配置值以及共享的部分視圖名稱。在這種情況下,將菜單放在您可以從單個主控制器調用的部分視圖中。但是,如果您真的想在將來提供自定義布局/外觀/主題,那么使用單個母版頁實現這一問題會更成問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/19/7205a276423b0a77f8a5b60d7e1c77b9.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com