问题
I currently have an ASP .NET MVC site that has two domains pointing to it (e.g. www.domainA.com and www.domainB.com). I would like to change all of my links to use www.domainA.com even if www.domainB.com is visited.
All of my urls are generated by the built-in Url.Action and Html.ActionLink methods.
I followed the instructions in a previous question but I can't quite get it working.
Here is my custom route:
public class MultipleDomainRoute : System.Web.Routing.Route
{
public MultipleDomainRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData path = new VirtualPathData(this, "http://www.domainA.com/" + values["controller"] + "/" + values["action"]);
return path;
}
}
Here is my custom route handler:
class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandler(requestContext);
}
}
Here is my RegisterRoutes method:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new MultipleDomainRoute("{controller}/{action}/{id}", new MyRouteHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
The route is working (i.e. not crashing my site) but when I run it, my urls are just appended to http://localhost... For example, I get "http://localhost:56822/http:/www.domainA.com/Home/Index".
Does anyone have any idea why this is not working?
回答1:
If you're using IIS 7, it may be better to implement this at web server level.
This way, the proper status code will be returned to the browser in the HTTP header and your MVC app can deal with its own routing without having to worry about domains and hostnames which, in most cases, is the web server's job.
There are two ways to achieve this
1. Via the IIS administration console
Open your website in the IIS console and select URL Rewrite and follow these steps:
- Create a new rule and set the match URL to wildcard and enter the pattern as your secondary domain
- Add a condition where {HTTP_HOST} is equal to the site's secondary domain.
- Set the "action type" to redirect, and enter the pattern you wish to redirect to. "http://mysite.com{R:1}" in this case would map everything after the IP address to its domain equivilent, so mysite.net/mycontent would redirect to mysite.com/mycontent
2. In your web.config
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to mydomain.com" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.net"/>
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
This approach will ensure your subdirectories are mapped and your site remains search engine friendly, with proper response codes being generated.
回答2:
Why not just forward domainB to domainA? I believe most registrars let your forward one domain to another.
回答3:
There are a couple of ways to do this.
In IIS7, as others have mentioned, you can do this pretty easily with the built-in URL Rewrite module. See
http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html
Or, add something like this to your web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
(from this site)
In IIS6, just add a new site for the secondary domain, and in the "Home Directory" tab set it to point to your primary domain:
That'll cause all requests to the host headers for the second site/secondary domain to redirect to the primary domain, including whatever paths and url parameters were included with the original site.
Note that if you don't use one of these top-level options, you'll probably have issues with cookies. Browsers don't like to send cookies from "www.a.com" to "www.b.com".
来源:https://stackoverflow.com/questions/9332599/how-to-force-a-single-domain-to-be-used-for-asp-net-mvc-site