简体   繁体   English

ASP.NET MVC路由-子域

[英]ASP.NET MVC Routing - subdomain

So my new web app, is dynamic web page and works based on the subdomain. 因此,我的新Web应用程序是动态网页,并且基于子域工作。 ie http://company1234.domain.com I then take the parameter "company1234" and do a web api call to get the page information to display. http://company1234.domain.com然后我使用参数“ company1234”并进行Web api调用以获取要显示的页面信息。

This all works very well. 这一切都很好。 However my issue is that if I need to call a parameter url ie http://company1234.domain.com/Home/GetClient?id=abcd or http://company1234.domain.com/Home/GetClientAccountDetails?model=abcd&acc=1234 但是我的问题是,如果我需要调用参数网址,即http://company1234.domain.com/Home/GetClient?id=abcdhttp://company1234.domain.com/Home/GetClientAccountDetails?model=abcd&acc= 1234

All my routing is destroyed, cant figure out how I rework this? 我所有的路由都被破坏了,无法弄清楚我该如何做?

public class SubdomainRoute : RouteBase
        {
            public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                if (httpContext.Request == null || httpContext.Request.Url == null)
                {
                    return null;
                }

                var host = httpContext.Request.Url.Host;
                var index = host.IndexOf(".");

                string[] segments = httpContext.Request.Url.PathAndQuery.TrimStart('/').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", "Home");
                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;
            }
        }

My Controller Methods 我的控制器方法

public ActionResult Index(string subdomain)
        {
//Go get custom page info
            var profile = GetProfile(subdomain);



            return View("Default", profile);
        }

public JsonResult GetClient(string id)
        {

//do stuff
}

My Global 我的全球

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.Add(new SubdomainRoute());

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");
            routes.MapRoute("Home",
                            "",
                            new { controller = "Application", action = "Login" });
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional}); 
        }

If I call http://company1234.domain.com/Home/GetClient 如果我拨打http://company1234.domain.com/Home/GetClient

Then this method is hit, however if i call http://company1234.domain.com/Home/GetClient?id=1234 it does not work 然后此方法被击中,但是如果我调用http://company1234.domain.com/Home/GetClient?id=1234 ,则此方法不起作用

Any ideas what I need to do to rework this and get it working? 有什么想法我需要做些什么来重做并使其起作用?

Routes need to do 2 things: 路线需要做两件事:

  1. Determine if they match the request. 确定它们是否符合请求。
  2. If they do, return a set of route values. 如果是这样,则返回一组路由值。

To handle the first requirement it is imperative that the route return null if it does not match the incoming request. 为了处理第一个要求,如果路由与传入请求不匹配,则必须返回null This tells the routing framework to check the next registered route in the route table. 这告诉路由框架检查路由表中的下一个已注册路由。

The problem is that your route does not check to ensure the URL that is passed is something it should handle, therefore it always matches the request and, since it is registered before all of your other routes, no other route can ever be reached. 问题在于您的路由不会检查以确保传递的URL是应该处理的内容,因此它始终与请求匹配,并且由于它是在所有其他路由之前注册的,因此无法访问其他路由。 Normally, the httpContext.Request.Path is checked to ensure it matches some criteria, and if not, null should be returned. 通常,将检查httpContext.Request.Path以确保其符合某些条件,如果不符合,则应返回null

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

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