【问题标题】:Asp.Net MVC Custom SubDomain Routing Not WrokingAsp.Net MVC 自定义子域路由不起作用
【发布时间】:2021-03-24 14:56:33
【问题描述】:

我有一个项目,您可以在其中注册具有特定 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

所以我将项目上传到 Plesk 主机上,并要求主机提供商为我的主机激活通配符并对 IIS 进行必要的更改,以便此路由系统正常工作。

即使我更改了“主机”文件,一切都在 localhost 上运行,因此我可以使用 localhost 上的真实域名测试项目并且它正在运行,但在远程主机上它无法运行并让我进入此页面:

Web 服务器的默认页面:

这是自定义路由类:

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:

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 }
            );
        }

“HomeController”的“Index”操作:

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();
            }
        }

我不知道我做错了什么或这里缺少什么,任何帮助将不胜感激

更新: 代码很好,因为更改主机上的 SSL 状态而导致的问题,它弄乱了通配符,要求托管服务提供商重新设置它,现在一切正常

【问题讨论】:

    标签: c# asp.net asp.net-mvc routes custom-routing


    【解决方案1】:

    更新:代码很好,由于更改主机上的 SSL 状态而导致的问题,它弄乱了通配符,要求托管服务提供商重新设置它,现在一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2011-04-02
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多