localhost上的子域出现错误请求-主机名无效错误
本文关键字:错误 请求 无效 主机 localhost | 更新日期: 2023-09-27 18:27:31
我正在使用MVC。我想用IIS测试本地主机上的子域。我所做的创建子域是:
-
我在windows主机文件中添加了一行
127.0.0.1 localhost 127.0.0.1 abc.localhost ::1 localhost
-
我将
applicationhost.config
编辑为:
<bindings>
<binding protocol="http" bindingInformation="*:59322:localhost" />
<binding protocol="http" bindingInformation="*:59322:abc.localhost" />
</bindings>
我在
RouteConfig.cs
中添加了以下类:public class SubdomainRoute : RouteBase { public override RouteData GetRouteData(HttpContextBase httpContext) { var host = httpContext.Request.Url.Host; var index = host.IndexOf("."); string[] segments = httpContext.Request.Url.PathAndQuery.Split('/'); if (index < 0) return null; var subdomain = host.Substring(0, index); string controller = (segments.Length > 0) ? segments[0] : "Home"; string action = (segments.Length > 1) ? segments[1] : "Index"; var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", controller); routeData.Values.Add("action", action); routeData.Values.Add("subdomain", subdomain); return routeData; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { //Implement your formating Url formating here return null; } }
现在要在控制器中获取子域名称:
public string subdomainName { get { string s = Request.Url.Host; var index = s.IndexOf("."); if (index < 0) { return null; } var sub = s.Split('.')[0]; if (sub == "www" || sub == "localhsot") { return null; } return sub; } }
我的索引方法是:
public string Index() { if (subdomainName == null) { return "No subdomain"; } return subdomainName; }
现在url http://localhost:59322/
工作正常。但是url http://abc.localhost:59322/
给出错误
错误的请求-主机名无效
HTTP错误400。请求主机名无效。
我做错了什么。为什么子域不工作?
我知道已经很晚了,但为了参考其他人,只需添加applicationhost.config
:
<bindings>
<binding protocol="http" bindingInformation="*:59322:" />
</bindings>