因此,我的新网站应用程序,是动态网页和基于子域的作品。 即http://company1234.domain.com 然后,我采取参数“company1234”并做一个web api调用来获取页面信息显示。ASP.NET MVC路由 - 子域名
这一切工作得很好。 但是我的问题是,如果我需要调用一个参数的URL即http://company1234.domain.com/Home/GetClient?id=abcd 或 http://company1234.domain.com/Home/GetClientAccountDetails?model=abcd&acc=1234
我所有的路由被破坏,无法弄清楚如何我返工呢?
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;
}
}
我的控制器方法
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
}
我的全球
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});
}
如果我打电话 http://company1234.domain.com/Home/GetClient
那么这种方法被击中,但如果我叫 http://company1234.domain.com/Home/GetClient?id=1234 它不起作用
任何想法,我需要做的,以重新工作,并得到它的工作?