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.

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.

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:

Asp.Net MVC Custom SubDomain Routing Not Wroking

Here is the custome route 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:
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” :
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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x