2013-05-08 52 views
0

在我的應用程序中,我允許用戶從我的應用程序重定向到htttps://www.google.co.in。RouteLink不適用於MVC 4

因爲我使用了RouteLink.I知道我們也可以通過actionLink來做到這一點。

CODE:

@Html.RouteLink("Google",null,"https","www.google.co.in", null, null, new { @style = "color:red" })   

生成的HTML

 <a style="color:red" href="https://www.google.co.in/Home/RouteLink">Google</a> 

而且我還添加路由

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
       name: "RouteLink", 
       url: "Home/RouteLink", 
       defaults: new { Controller = "Home", action = "RouteLink" }); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = 
      UrlParameter.Optional } 
     ); 

    } 

所以我的問題是,爲什麼我得到的錨標記和上面一樣?如果RouteLink不允許重定向到另一個域,而不是爲什麼它重載了接受主機名和協議的方法?

+0

當常規超鏈接('a')應該做什麼時,RouteLink會有什麼用? – shahkalpesh 2013-05-08 05:56:49

+0

是的,我也可以使用簡單的錨點標籤來做到這一點,我也可以使用@ Html.ActionLink()來做到這一點,但爲什麼@ html.RouteLink有一個重載的方法,它符合主機名和協議?以及爲什麼我無法使用RouteLink重定向到另一個域? – 2013-05-08 06:01:23

+0

我不知道主機名背後的原因。但是,RouteLink文檔說它爲虛擬路徑生成鏈接。 – shahkalpesh 2013-05-08 07:43:37

回答

0

所以我的問題是,爲什麼我得到的錨標記和上面一樣

因爲你明確告訴它,以便在第三和RouteLink的第四個參數。

如果RouteLink不允許重定向到另一個域,而不是爲什麼它重載了接受主機名和協議的方法?

因爲這不是你用它的目的。 RouteLink和它的姐妹方法ActionLink用於在應用程序中生成有效(或安全)的鏈接。

如果你想鏈接到外部應用程序/網站,那麼只需使用錨標籤。

+0

所以你的意思是我不能使用routeLink來做到這一點? – 2013-05-08 06:07:02

+0

對於外部網站,您不能。 [看這篇博客文章瞭解更多使用方法](http://haacked.com/archive/2010/11/21/named-routes-to-the-rescue.aspx) – 2013-05-08 06:12:26

+0

比爲什麼它有重載方法喜歡@ Html.RouteLink(linkText,routeName,protocol,hostName,fragment,routeValues,htmlAttributes) – 2013-05-08 06:16:05