使用子域作为参数 [英] Using the subdomain as a parameter

查看:68
本文介绍了使用子域作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.net MVC(5.2)网站,该网站使用多个子域运行,其中子域的名称是数据库中客户端的名称.基本上,我想做的是在操作方法中使用子域作为变量,以使我能够从数据库中获取正确的数据.

I’ve got an ASP.net MVC (5.2) site that runs using several subdomains, where the name of the subdomain is the name of a client in my database. Basically what I want to do is use the subdomain as a variable within my action methods to allow me to get the correct data from my database.

几年前我做了类似的事情,但是它很混乱而且不直观,所以想知道是否有比我以前使用的更好的方法.这是我以前做过的事情:

I did something similar a few years back, but it’s messy and not intuitive, so was wondering if there’s a better way to do it than I was using before. Here’s what I did before:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    Session["subdomain"] = GetSubDomain(Request.Url);
}

private static string GetSubDomain(Uri url) {
    string host = url.Host;
    if (host.Split('.').Length > 1) {
        int index = host.IndexOf(".");
        string subdomain = host.Substring(0, index);
        if (subdomain != "www") {
            return subdomain;
        }
    }
    return null;
}

如果子域不是"www",那么哪个基本上为会话变量分配了一个键,但是我对这种方式实在不满意,因为它依赖于我知道会话可能包含这个神奇的值!

Which basically assigned a key to the session variable if the subdomain was anything other than "www", but I’m really not happy with this way of doing it as it relies on me knowing that the session might contain this magic value!

理想情况下,我希望能够创建一个属性来装饰我的类/方法,从而提取子域,然后允许我在我的操作方法中包括一个"subdomain"参数,该参数将包含提取的值通过属性.那有可能吗?

Ideally I’d like to be able to create an attribute that I can decorate my classes/methods with that would extract the subdomain and then allow me to include a "subdomain" parameter in my action method that would contain the value extracted by the attribute. Is that even possible?

如果无法做到这一点,是否有一种更好的方式来执行我现在正在做的事情而不必依赖会话?

If that can’t be done, is there a better way of doing what I’m doing now without having to rely on the session?

谢谢

迪伦

推荐答案

您的权限并不需要存储在Session中,恕我直言,我应该将其重构为自己的类并使用HttpContext.Current .

Your right this doesn't need to be stored in Session and IMHO shouldn't be, I would refactor this out into its own class and use HttpContext.Current.

public interface ISubDomainProvider
{
    string SubDomain { get; set; }
}


public class SubDomainProvider : ISubDomainProvider
{
    public SubDomainProvider()
    {
        string host = HttpContext.Current.Request.Url.Host; // not checked (off the top of my head
        if (host.Split('.').Length > 1) 
        {
            int index = host.IndexOf(".");
            string subdomain = host.Substring(0, index);
            if (subdomain != "www") 
            {
                SubDomain = subdomain;
            }
        }
    }

    public string SubDomain { get; set; }
}

您选择如何使用它,如果您使用的是IoC容器,则只是通过构造函数将此类注入控制器中的情况,我喜欢这样做,因为它更易于模拟和单元测试.当然,您仍然可以这样做:

You choose how to use it, if your using an IoC container it would just be a case of injecting this class into your controller via the constructor, I like this because it is easier to Mock and Unit Test. Of course you can still do this:

public class SomeController : Controller
{
    private readonly ISubDomainProvider _subDomainProvider;
    public SomeController()
    {
        _subDomainProvider = new SubDomainProvider();
    }
} 

您甚至可以创建自己的抽象Controller类:

You could even create you own abstract Controller Class:

public abstract class MyAbstractController : Controller
{
    public MyAbstractController()
    {
        SubDomain = new SubDomainProvider();
    }

    protected string SubDomain {get; set; }
}

public class SomeController : MyAbstractController
{
    public ActionResult SomeAction()
    {
        // access the subdomain by calling the base base.SubDomain 
    }
}

这篇关于使用子域作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆