![](/img/trans.png)
[英]How to force client side validation of a single element in ASP.NET MVC 3 (jQuery validation)?
[英]How to force a single domain to be used for ASP.NET MVC site?
我目前有一個ASP .NET MVC站點,該站點有兩個指向它的域(例如www.domainA.com和www.domainB.com)。 我想將所有鏈接更改為使用www.domainA.com,即使已訪問www.domainB.com。
我所有的網址都是由內置的Url.Action和Html.ActionLink方法生成的。
我按照上一個問題中的說明進行操作,但無法完全正常工作。
這是我的自定義路線 :
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;
}
}
這是我的自定義路由處理程序:
class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandler(requestContext);
}
}
這是我的RegisterRoutes方法:
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
);
}
該路由有效(即不會使我的網站崩潰),但是當我運行它時,我的URL只是附加到http:// localhost ...例如,我得到了“ http:// localhost:56822 / http:/ www。 domainA.com/Home/Index “。
有誰知道為什么這不起作用?
如果您使用的是IIS 7,則最好在Web服務器級別實現此功能。
這樣,正確的狀態代碼將在HTTP標頭中返回到瀏覽器,並且您的MVC應用程序可以處理其自己的路由,而不必擔心域和主機名(在大多數情況下,這是Web服務器的工作)。
有兩種方法可以實現此目的
1.通過IIS管理控制台
在IIS控制台中打開您的網站,然后選擇URL Rewrite,然后執行以下步驟:
2.在您的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>
這種方法將確保您的子目錄被映射,並且您的站點保持搜索引擎友好,並生成正確的響應代碼。
為什么不將domainB轉發到domainA? 我相信大多數注冊服務商都會將您的域名轉發給另一域名。
有兩種方法可以做到這一點。
正如其他人提到的那樣,在IIS7中,您可以使用內置的URL重寫模塊輕松完成此操作。 看到
http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html
或者,將類似以下內容添加到您的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>
(來自此站點 )
在IIS6中 ,只需為輔助域添加一個新站點,然后在“主目錄”選項卡中將其設置為指向您的主域:
這將導致對第二個站點/第二個域的主機頭的所有請求都重定向到主域,包括原始站點中包括的所有路徑和url參數。
請注意,如果您不使用這些頂級選項之一,則cookie可能會出現問題。 瀏覽器不喜歡將Cookie從“ www.a.com”發送到“ www.b.com”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.