Local MV上的ASP MVC \子域名

时间:2016-05-10 15:10:48

标签: asp.net asp.net-mvc-4

我正在使用IIS Express本地测试ASP MVC EF应用程序。我正在尝试确定HTTP请求中的子域但我收到“错误请求 - 无效主机名”错误

我已经查看了一些有关更改主机文件但没有解决错误的答案。

在我的HomeController中,我有这种方法来简单地打印请求的URL

  

public string Index(){var hn = Request.Headers [“HOST”];返回   hn.ToString(); }

查看localhost时 - >页面加载

查看tenant1.localhost时的

- >错误的要求

如果有人能指出我正确的方向来解决此错误,我们将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

默认情况下,IIS Express仅允许localhost。为了允许其他域在开发中访问您的网站,您必须在%USERPROFILE%\Documents\IISExpress\config\applicationhost.config中手动添加绑定。找到与您的应用程序对应的<site>节点,并在<bindings>节点中添加新的<binding>。将有默认绑定用作指南。不过,请不要使用默认绑定,否则您最终可能会在Visual Studio中出现奇怪的错误。

此外,我建议使用类似localtest.me的内容来测试子域名,因为subdomain.localhost结构不能正常运行,因为大多数事情会尝试假设{{1}是tld。基本上,localtest.me是一个已设置为通配符的域,将所有内容路由到127.0.0.1(localhost)。因此,您可以选择并使用您想要的任何子域。对于它的价值,这对于测试Facebook等开发中的第三方集成非常有效,因为它们不允许本地主机。

答案 1 :(得分:0)

我尝试Paul Taylor上面的答案非常好,但这对我来说并不完全有效。 我使用Route类的实现。

将您的自定义域添加到C:/Windows/System32/drivers/etc/hosts文件

  • 127.0.0.1 subdomain.localhost.com

DomainData.cs

public class DomainData
{
  public string Protocol { get; set; }
  public string HostName { get; set; }
  public string Fragment { get; set; }
}

DomainRoute.cs

public class DomainRoute : Route
{
  private Regex domainRegex;
  private Regex pathRegex;

  public string Domain { get; set; }

  public DomainRoute(string domain, string url, RouteValueDictionary defaults)
    : base(url, defaults, new MvcRouteHandler())
{
    Domain = domain;
}

public DomainRoute(string domain, string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
    : base(url, defaults, routeHandler)
{
    Domain = domain;
}

public DomainRoute(string domain, string url, object defaults)
    : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
{
    Domain = domain;
}

public DomainRoute(string domain, string url, object defaults, IRouteHandler routeHandler)
    : base(url, new RouteValueDictionary(defaults), routeHandler)
{
    Domain = domain;
}

public override RouteData GetRouteData(HttpContextBase httpContext)
{
    // Build regex
    domainRegex = CreateRegex(Domain);
    pathRegex = CreateRegex(Url);

    // Request information
    string requestDomain = httpContext.Request.Headers["host"];
    if (!string.IsNullOrEmpty(requestDomain))
    {
        if (requestDomain.IndexOf(":") > 0)
        {
            requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":"));
        }
    }
    else
    {
        requestDomain = httpContext.Request.Url.Host;
    }
    string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) +
                         httpContext.Request.PathInfo;

    // Match domain and route
    Match domainMatch = domainRegex.Match(requestDomain);
    Match pathMatch = pathRegex.Match(requestPath);

    // Route data
    RouteData data = null;
    if (domainMatch.Success && pathMatch.Success && requestDomain.ToLower() != "tg.local" &&
        requestDomain.ToLower() != "tg.terrasynq.net" && requestDomain.ToLower() != "www.townsgossip.com" &&
        requestDomain.ToLower() != "townsgossip.com")
    {
        data = new RouteData(this, RouteHandler);

        // Add defaults first
        if (Defaults != null)
        {
            foreach (KeyValuePair<string, object> item in Defaults)
            {
                data.Values[item.Key] = item.Value;
            }
        }

        // Iterate matching domain groups
        for (int i = 1; i < domainMatch.Groups.Count; i++)
        {
            Group group = domainMatch.Groups[i];
            if (group.Success)
            {
                string key = domainRegex.GroupNameFromNumber(i);

                if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
                {
                    if (!string.IsNullOrEmpty(group.Value))
                    {
                        data.Values[key] = group.Value;
                    }
                }
            }
        }

        // Iterate matching path groups
        for (int i = 1; i < pathMatch.Groups.Count; i++)
        {
            Group group = pathMatch.Groups[i];
            if (group.Success)
            {
                string key = pathRegex.GroupNameFromNumber(i);

                if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
                {
                    if (!string.IsNullOrEmpty(group.Value))
                    {
                        data.Values[key] = group.Value;
                    }
                }
            }
        }
    }

    return data;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
    return base.GetVirtualPath(requestContext, RemoveDomainTokens(values));
}

public DomainData GetDomainData(RequestContext requestContext, RouteValueDictionary values)
{
    // Build hostname
    string hostname = Domain;
    foreach (KeyValuePair<string, object> pair in values)
    {
        hostname = hostname.Replace("{" + pair.Key + "}", pair.Value.ToString());
    }

    // Return domain data
    return new DomainData
    {
        Protocol = "http",
        HostName = hostname,
        Fragment = ""
    };
}

private Regex CreateRegex(string source)
{
    // Perform replacements
    source = source.Replace("/", @"\/?");
    source = source.Replace(".", @"\.?");
    source = source.Replace("-", @"\-?");
    source = source.Replace("{", @"(?<");
    source = source.Replace("}", @">([a-zA-Z0-9_\-]*))");

    return new Regex("^" + source + "$");
}

private RouteValueDictionary RemoveDomainTokens(RouteValueDictionary values)
{
    var tokenRegex =
        new Regex(
            @"({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?");
    Match tokenMatch = tokenRegex.Match(Domain);
    for (int i = 0; i < tokenMatch.Groups.Count; i++)
    {
        Group group = tokenMatch.Groups[i];
        if (group.Success)
        {
            string key = group.Value.Replace("{", "").Replace("}", "");
            if (values.ContainsKey(key))
                values.Remove(key);
        }
    }

    return values;
  }
}

参考:Domain Routing in MVC5

相关问题