使用单一代码库支持多个网站有哪些方法?
我正在编写一个非常简单的 ASP.NET MVC Web 应用程序:只有几个 CRUD 页面、一些客户端可以浏览文档的文件夹以及 3 或 4 个角色。该网站将用于 B2B 场景,每个客户都将拥有自己的“自己”网站。
此时,网站中从客户端到客户端唯一会发生变化的是内容(即文档以及他们将看到的数据行)。如果是这种情况,管理所有客户角色的最佳方法是什么?我正在寻找最简单的解决方案,因为这是一个概念证明,我现在不想投入大量时间。
如果改变的不仅仅是内容怎么办?也许有些客户会想要一些自定义静态页面。此时,我唯一的选择是复制整个网站吗?我对此持怀疑态度,因为如果我有很多客户,维护就会变得困难。
我将不胜感激任何帮助......我只是不想搬起石头砸自己的脚;我确信以前有人这样做过。
I'm writing a pretty straight forward ASP.NET MVC web app: only a couple of CRUD pages, some folders where clients can browse documents and just 3 or 4 roles. The website will be used in a B2B scenario, where every client will have their "own" website.
At this point, the only thing that will change in the website, from client to client is the content (ie. the documents, and the rows of data they'll see). If this is the case, what's the best way to manage roles across all of my clients? I'm looking for the simplest possible solution because this is a proof of concept and I don't want to invest a lot of time right now.
What if it's not just the content that changes? Maybe some clients will want a few custom static pages. At this point, is my only option replicating the entire website? I'm leery of this because it'll become hard to maintain if I get a lot of clients.
I'd appreciate any help... I just don't want to shoot myself in the foot; I'm sure someone has done this before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 IIS 中为每个客户端创建虚拟目录,所有目录都指向我的 ASP.NET 代码所在的同一文件夹。
这使我能够支持几十个几乎相同的“网站”,每个网站都有自己的数据库,其形式基本相同,仅数据不同。
因此,我的站点 URL 如下所示:
我为此制定了两个关键的实现细节:
我使用虚拟目录文件夹名称来确定我的代码从哪个 DSN 读取。这是通过创建一个简单的静态方法将文件夹名称注入 DSN 字符串模板来完成的。如果您想使用相同数据库来存储每个人的数据,您可以在查询中使用文件夹名称作为默认过滤器。
我将每个网站的设置(页眉和页脚、选项、自定义报告的链接等)存储在每个数据库(键、值)的简单“设置”表中,而不是存储在 web.config 中(其中是共享的)。这使我能够随着时间的推移扩展代码库,为每个客户定制体验,而无需分叉代码。
对于用户身份验证,我使用基本身份验证,并将用户名、密码和角色保存在每个数据库的表中。
重要的是,如果您为每个客户端的内容使用不同的 SQL Server 数据库,则需要编写对数据库表、索引等的任何更改的脚本,并同时将它们应用到所有数据库(当然是在测试之后)。执行此操作的一种简单方法是维护一个 Excel 工作表,其中包含数据库名称表和顶部的大“SQL”单元格。在每个数据库名称旁边,创建一个公式“USE databasename;”然后在顶部连接 SQL 代码。
I create Virtual Directories in IIS for each client, all pointed back to the same folder where my ASP.NET code resides.
This allows me to support several dozen nearly-identical "web sites," each with their own database that is basically identical in form, only differs in data.
So, my site URLs look like:
There are two key implementation details I worked out for this:
I use the Virtual Directory folder name to determine which DSN my code reads from. This is accomplished by creating a simple static method that injects the folder name into a DSN string template. If you want to use the same database to store everyone's data, you can use the folder name as a default filter in your queries.
I store the settings for each web site (headers and footers, options, links to custom reports, etc.) in a simple "settings" table in each database (key, value) rather than in the web.config (which is shared). This allows me to extend the code base over time to customize the experience for each client without forking the code.
For user authentication, I use Basic authentication, and I keep usernames, passwords, and roles in a table in each database.
The important thing is that if you use different SQL Server databases for each client's content, you need to script any changes to your database tables, indexes, etc. and apply them across all databases at the same time (after testing of course). One simple way to do this is to maintain an Excel sheet with a table of database names and a big "SQL" cell at the top. Beside each database name, create a formula to "USE databasename;" and then concat the SQL code at the top.
我不确定这是否完全回答了您的问题,但就维护自定义“静态”页面而言,我发现自己在客户的 MVC 网站上实现了一个系统,客户可以从其管理控制面板创建“页面”,并且每个页面都有“PageContent”实体的集合,其中包含标题和 HTML 内容字段(使用 WYISWYG 编辑器填充)。创建页面后,MVC 应用程序会映射 http://yoursite.com/用户指定的页面/页面 URL 到该页面并在那里呈现其内容。显然,这些页面是动态的,但据客户所知,他们几乎不费吹灰之力就创建了一个全新的自定义页面。
I'm not sure if this answers your question completely, but as far as maintaining custom "static" pages I found myself implementing a system on a client's MVC website where the client can create "Pages" from their admin control panel and each Page has a collection of "PageContent" entities which consist of a Title and and HTML content field (populated using a WYISWYG editor). Upon creating a page the MVC application maps http://yoursite.com/Page/Page-Url-Specified-By-The-User to that page and renders its content there. Obviously, the pages are dynamic, but as far as the client can tell they have created a brand new custom page with little or no effort.