在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.

最后,我认为另一种解决方案是保留单个布局文件,但反转共享局部视图中的内容。这意味着您可以将标题图像存储为配置值以及共享的部分视图名称。在这种情况下,将菜单放在您可以从单个主控制器调用的部分视图中。但是,如果您真的想在将来提供自定义布局/外观/主题,那么使用单个母版页实现这一问题会更成问题。

智能推荐

注意!

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



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

赞助商广告