路由与MVC3一个子 [英] Routing a subdomain with MVC3
问题描述
我想创造一些我的客户的服务,将让他们有一个MVC3网站,我已经建立有自己的网页。
I am wanting to create a service for some of my customers that will allow them to have there own webpage on a MVC3 site I have built.
我想一个企业要能够有一个网址,看起来像www.BusinessName.mydomain.com
I want a business to be able to have a url that looks like www.BusinessName.mydomain.com
是否有可能创建MVC3内的路线,让我做到这一点?
Is it possible to create a route within MVC3 that will allow me to do this?
感谢您的帮助。
好了,所以我觉得我已经进一步得到了一点。
Ok so I think I have gotten a little further.
理想我希望能够有IIS中的子域通配符但是从我做它看起来这是不可能的研究。
Ideally I want to be able to have a wildcard for the subdomain in IIS however from the research I have done it looks like this isn't possible.
我手动添加www.mycompany.mydomain.com到IIS和更新我的global.ascx包含此
I have manually added www.mycompany.mydomain.com into IIS and updated my global.ascx to contain this
public class SubDomainRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var url = httpContext.Request.Headers["HOST"];
var subDomain = Helpers.Helpers.GetSubDomain(url);
if (subDomain==null)
return null;
var routeData = new RouteData(this, new MvcRouteHandler());
switch (subDomain)
{
case "mycompany":
routeData.Values.Add("controller", "SubDomain");
routeData.Values.Add("action", "Index");
break;
default:
routeData.Values.Add("controller", "Home");
routeData.Values.Add("action", "Index");
break;
}
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
所以,现在我可以浏览www.mycompanyname.mydomain.com和正确的控制器被装入。
So now I can browse www.mycompanyname.mydomain.com and the correct controller is loaded.
我现在有2个问题。
如果我浏览到www.mistake.mydomain.com我得到一个502错误。如果www.mistake.mydomain.com没有找到什么我可以在IIS做重定向到www.mydomain.com?
If I browse to www.mistake.mydomain.com I get a 502 error. What can I do in IIS to redirect to www.mydomain.com if www.mistake.mydomain.com isn't found?
一旦我能浏览到www.mycompanyname.mydomain.com所有的链接我的主菜单中,现在指向例如www.mycompanyname.mydomain.com/contactus这是不对的,我怎么保证菜单项不使用URL中的子?
Once I have browsed to www.mycompanyname.mydomain.com all of the links within my main menu are now pointing to e.g. www.mycompanyname.mydomain.com/contactus which is wrong, how do I ensure the menu items are not using the subdomain within the url?
推荐答案
我建议稍微不同的方法 - 唯一需要注意的是,这个网站必须在IIS中的唯一的一个。因为IIS不支持通配符主机头,它必须是默认的Web站点。
I'd suggest a slightly different approach - only caveat is that this web site must be the only one on IIS. Because IIS doesn't support wildcard host headers, it has to be Default Web site.
这样,你会得到所有的请求(包括www.mistake.mydomain.com)到应用程序的前门,你可以适当地处理它们。
This way, you will get all requests (including www.mistake.mydomain.com) to your application's front door, and you can handle them appropriately.
在你的MVC应用程序,请不要使用路由子域,使用的Application_BeginRequest。在那里,你做你有Request.Headers办[HOST],并要将他们送他们什么。
In your MVC application, don't use routing for subdomains, use Application_BeginRequest. There you do what you have to do with Request.Headers["HOST"] and send them where you want them.
我看到它的方式,路线是为了用于路由用户当他到现场,而不是决定哪些网站的用户访问...
The way I see it, Routes are meant to be used for routing user when he gets to the site, not to decide which site user is visiting...
这篇关于路由与MVC3一个子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!