如何开始使用多租户 MVC 应用程序

发布于 2024-08-22 04:52:57 字数 790 浏览 9 评论 0原文

我搜索了示例并找到了几个,但它们都是大型项目。我正在寻找一些有关如何开始构建 MVC 多租户应用程序的示例。我认为,第一部分是破译网址。

在 ASP.Net 中,我就是这样做的。我通过查看 DNN 代码得到了这一点。 我如何在 MVC 中执行相同的操作?

Global.asax

private void Application_BeginRequest(Object source, EventArgs e)
{
  HttpApplication application = (HttpApplication)source;
  HttpContext context = application.Context;

  string domainName = string.Empty;
  // domaName now contains 'example' if application.Request was www.example.com
  domainName = GetDomainName(application.Request);

  // Using domain, get the info for example from the database
  object myPortal = // get from database
  // Save in context for use on other pages
  context.Items.Add("PortalSettings", myPortal);
}

然后在我的基本页面中,我从上下文中获取值。

I have searched for examples and found several but they are whole large projects. I am looking for some sample on how to get started building an MVC multi-tenant application. I think, the first part would be to decipher the url.

In ASP.Net this is how I did it. I got this from looking at DNN code. How would I do the same in MVC?

Global.asax

private void Application_BeginRequest(Object source, EventArgs e)
{
  HttpApplication application = (HttpApplication)source;
  HttpContext context = application.Context;

  string domainName = string.Empty;
  // domaName now contains 'example' if application.Request was www.example.com
  domainName = GetDomainName(application.Request);

  // Using domain, get the info for example from the database
  object myPortal = // get from database
  // Save in context for use on other pages
  context.Items.Add("PortalSettings", myPortal);
}

Then in my basepage I get the value from the context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-08-29 04:52:57

我认为更强大的方法是定义自定义路线。在该自定义路由中,您可以提取域并将其放入路由值中。

然后,您可以拥有基本控制器(如 Josh 所描述的),它定义了一个 Domain 属性等,并将该值存储在那里以方便使用(或者只是根据需要提取它;无论哪种方式)。

通过像这样将其拉入前面的路由值,您可以在请求路径上的应用程序中的任何位置使用该信息,而不仅仅是在控制器中,因此您可以通过这种方式获得更多的重用。例如,您可以在自定义的类似授权的过滤器中使用它来处理用户对该域的权限,等等。

I think an even more robust means would be to define a custom route. In that custom route is where you extract the domain and put it into the route values.

You then can have the base controller (as Josh described) which defines a Domain property or the like and stores that value there for convenience (or just extracts it on demand; either way).

By pulling it into the route values up front like that, you can make use of that information anywhere in the app along the request path, not just in the controller, so you get more re-use out of it that way. You can, for example, make use of it in a custom Authorize-like filter to handle the user's rights to that domain, and so on.

眼藏柔 2024-08-29 04:52:57
  1. 获取域名。您的 DNN 代码走在正确的轨道上。只需在调试器中查看 Request 静态变量即可;那里有各种各样很酷的东西。
  2. 您可能需要一个用户存储。我使用自定义数据库,但您可以使用 Microsoft 会员资格提供程序和配置文件提供程序。使域成为用户的属性,或者成为组织的属性,并使组织成为用户的属性。
  3. 将用户的域加密存储在 cookie 中。读取请求开头的 cookie,并使用户有权访问该组织/域。
  4. 创建一个扩展 Controller 的 BaseController,然后让所有控制器继承它。在 BaseController 中,重写 OnActionExecuting。与 Global.asax.cs 的 Begin_request 相比,这是一个更容易进行初始请求操纵的地方,因为您可以定义每个控制器都可用的受保护成员。
  1. Get the domain name. You are on the right track with the DNN code. Just poke around the Request static variable in the debugger; there's all kinds of cool stuff there.
  2. You'll probably need a user store. I use a custom database, but you could use the Microsoft membership provider and profile provider. Make the domain a property of the user, or a property of an organization, and the organization a property of the user.
  3. Store the user's domain in the cookie, encrypted. Read the cookie at the beginning of the request, and make the user has access to that org/domain.
  4. Make a BaseController that extends Controller, then have all your controllers inherit from it. In the BaseController, override OnActionExecuting. This is a much easier place to do your initial request rigging than the Global.asax.cs's Begin_request, because you can define protected members which will be available form every controller.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文