编程语言
首页 > 编程语言> > c#-本地主机上的子域给出错误请求-无效的主机名错误

c#-本地主机上的子域给出错误请求-无效的主机名错误

作者:互联网

我正在使用MVC.我想使用IIS在localhost上测试子域.我创建子域的工作是:

>我在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中添加了以下类:

    公共类SubdomainRoute:RouteBase
        {
            公共重写RouteData GetRouteData(HttpContextBase httpContext)
            {
                var host = httpContext.Request.Url.Host;
                var index = host.IndexOf(“.”);
                string []段= httpContext.Request.Url.PathAndQuery.Split(‘/’);
                如果(索引< 0)
                    返回null;
                var subdomain = host.Substring(0,index);
                字符串控制器=(segments.Length> 0)? segment [0]:“主页”;
                字符串操作=(segments.Length> 1)? segment [1]:“索引”;
                var routeData = new RouteData(this,new MvcRouteHandler());
                routeData.Values.Add(“ controller”,controller);
                routeData.Values.Add(“ action”,action);
                routeData.Values.Add(“ subdomain”,subdomain);
                返回routeData;
            }
            公共重写VirtualPathData GetVirtualPath(RequestContext requestContext,RouteValueDictionary值)
            {
                //在此处实现格式化网址格式
                返回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;
            }
        }

>我的索引方法是:

公共字符串Index()
        {
            如果(subdomainName == null)
            {
                返回“无子域”;
            }
            返回subdomainName;
        }

现在,URL http:// localhost:59322 /可以正常工作.但是网址http://abc.localhost:59322 /给出了错误

Bad Request – Invalid Hostname

HTTP Error 400. The request hostname is invalid.

我做错了.为什么子域无法正常工作?

解决方法:

我知道已经很晚了,但要参考其他内容,只需添加applicationhost.config即可:

<bindings>
     <binding protocol="http" bindingInformation="*:59322:" />
</bindings>

标签:asp-net-mvc-routing,subdomain,asp-net,c,asp-net-mvc
来源: https://codeday.me/bug/20191119/2034909.html