如何开始使用多租户 MVC 应用程序
我搜索了示例并找到了几个,但它们都是大型项目。我正在寻找一些有关如何开始构建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为更强大的方法是定义自定义路线。在该自定义路由中,您可以提取域并将其放入路由值中。
然后,您可以拥有基本控制器(如 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.