简体   繁体   English

Asp.Net MVC 自定义子域路由不工作

[英]Asp.Net MVC Custom SubDomain Routing Not Wroking

I have a project in which you are able to register posts with specific UID and other people would be able to access that post by using that post's UID as a subdomain.我有一个项目,您可以在其中注册具有特定 UID 的帖子,其他人可以通过使用该帖子的 UID 作为子域来访问该帖子。

Let's say you register a post by this UID => "test"
then others can access that post with 2 URLs

fisrt => test.mydomain.com
second => mydomain.com/home/Post?Id=test

So i uploaded the project on a Plesk host and asked the hosting provider to activate the wild card for my host and do the required change to IIS so this routing system works.因此,我将项目上传到 Plesk 主机上,并要求托管服务提供商为我的主机激活通配符并对 IIS 进行必要的更改,以便此路由系统正常工作。

Everything is working on localhost even i changed the "host" file so i can test the project with the real domain name on localhost and it's working but on the remote host it's not working and gets me to this page:一切都在本地主机上运行,即使我更改了“主机”文件,所以我可以在本地主机上使用真实域名测试项目并且它正在运行,但在远程主机上它不起作用并让我进入这个页面:

Web Server's Default Page: Web 服务器默认页面:

1

Here is the custome route class:这是定制路线class:

namespace _360V.App_Start
{
    public class TRoute : RouteBase
    {
        API api = new API();
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            if (httpContext.Request == null || httpContext.Request.Url == null)
            {
                api.log(new Log() { Des = "CustomRouteInfo, Null Request & RequestUrl" });
                return null;
            }

            string host = httpContext.Request.Url.Host;
            string[] hostSplit = host.Split('.');
            string subDomain = "";

            if (hostSplit.Length < 2)
            {
                // go home
                return null; 
            }else if (hostSplit.Length == 3)
            {
                if (hostSplit[0].ToLower() == "www")
                    return null;//go home
                else
                    subDomain = hostSplit[0];
            }else if (hostSplit.Length == 4)
            {
                if (hostSplit[0] == "www")
                    subDomain = hostSplit[1];
                else
                    return null;//go home
            }
            else
            {
                return null;//go home
            }

            string[] segments = httpContext.Request.Url.PathAndQuery.TrimStart('/').Split('/');

            string controller = (segments.Length > 0) ? segments[0] : "Home";
            string action = (segments.Length > 1) ? segments[1] : "Index";

            if (string.IsNullOrWhiteSpace(controller))
                controller = "Home";
            if (string.IsNullOrWhiteSpace(action))
                action = "Index";

            if (segments.Length >= 3 && (segments[1] == "Complex" || segments[1] == "Post"))
            {
                string u = httpContext.Request.Url.Authority;
                string scheme = httpContext.Request.Url.Scheme;
                string[] sp = u.Split('.');
                u = sp[sp.Length - 2] + "." + sp[sp.Length - 1];
                if (segments[1] == "Complex")
                    httpContext.Response.RedirectPermanent(scheme + "://" + u + "/Home/Complex?Id=" + subDomain);
                else if (segments[1] == "Post")
                    httpContext.Response.RedirectPermanent(scheme + "://" + u + "/Home/Post?Id=" + subDomain);

                api.log(new Log() { Des = "Segment Lenght >= 3, Segment[1] => " + segments[1] });
                return null;
            }

            var routeData = new RouteData(this, new MvcRouteHandler());
            routeData.Values.Add("controller", controller); //Goes to the relevant Controller  class
            routeData.Values.Add("action", action); //Goes to the relevant action method on the specified Controller
            routeData.Values.Add("subdomain", subDomain); //pass subdomain as argument to action method

            return routeData;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            //Implement your formating Url formating here
            return null;
        }
    }
}

RouteConfig.cs: RouteConfig.cs:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(new TRoute());

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

"Index" Action of "HomeController": “HomeController”的“索引”操作:

API api = new API();
        public ActionResult Index(string id, string subdomain)
        {
            string t = "";
            try { t = Request.Cookies[api.TokenTitle].Value.ToString(); }
            catch { }

            User cu = new User() { ID = -1 };
            ViewData.Add("max", api.getFilterRanges());

            if (!string.IsNullOrWhiteSpace(subdomain))
            {
                api.log(new Log() { Des="Home Index, SubDomain => "+subdomain});
                string u = Request.Url.Authority;
                string tmpU = u;
                string scheme = Request.Url.Scheme;
                string[] sp = u.Split('.');
                u = sp[sp.Length - 2] + "." + sp[sp.Length - 1];

                if (api.PostExist(subdomain))
                {
                    api.log(new Log() { Des = "(tmpU => "+tmpU+") Home Index, PostExist => Redirecting To => \""+ scheme + "://" + u + "/Home/Post?Id=" + subdomain + "\" | SubDomain => " + subdomain });
                    Response.RedirectPermanent(scheme + "://" + u + "/Home/Post?Id=" + subdomain);
                    return RedirectPermanent(scheme + "://" + u + "/Home/Post?Id=" + subdomain);
                    //return Post(subdomain);
                }
                else if (api.ComplexExist(subdomain))
                {
                    api.log(new Log() { Des = "(tmpU => " + tmpU + ") Home Index, ComplexExist => Redirecting To => \"" + scheme + "://" + u + "/Home/Complex?Id=" + subdomain + "\" | SubDomain => " + subdomain });
                    Response.RedirectPermanent(scheme + "://" + u+"/Home/Complex?Id="+ subdomain);
                    return RedirectPermanent(scheme + "://" + u + "/Home/Complex?Id=" + subdomain);
                }
                else
                {
                    api.log(new Log() { Des = "(tmpU => " + tmpU + ") Home Index, Not Found => Redirecting To => \"" + scheme + "://" + u + "\" | SubDomain => " + subdomain });
                    Response.RedirectPermanent(scheme+"://" + u);
                    return RedirectPermanent(scheme + "://" + u);
                }

            }

            if (api.isTokenValid(t))
            {
                cu = api.getUserInfo(t);
                ViewData.Add("cu", cu);
                return View();
            }
            else
            {
                var c = new HttpCookie(api.TokenTitle);
                c.Expires = DateTime.Now.AddDays(-1);
                Response.SetCookie(c);
                ViewData.Add("cu", cu);
                return View();
            }
        }

I have no idea what im doing wrong or whats missing here, any help would be appreciated <3我不知道我做错了什么或这里缺少什么,任何帮助将不胜感激<3

Update: The code was fine the problem caused because of changing the status of SSL on the host and it messed the wildcard, asked the hosting provider to set it again and everything is working now更新:代码很好,因为更改主机上 SSL 的状态而导致的问题,它弄乱了通配符,要求托管服务提供商再次设置它,现在一切正常

Update: The code was fine the problem caused because of changing the status of SSL on the host and it messed the wildcard, asked the hosting provider to set it again and everything is working now更新:代码很好,因为更改主机上 SSL 的状态而导致的问题,它弄乱了通配符,要求托管服务提供商再次设置它,现在一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM