2008-08-08 48 views

回答

5

我們正在尋求在我們的下一個版本中包括對此的支持。

+0

這是要在dev10發佈的目標嗎? – ajma 2009-05-19 07:59:22

1

簡短的回答是:不可以。在ASP.NET MVC Preview 3中,沒有一種方法可以在動作鏈接中包含錨點。與Rails的url_for:anchor不同,UrlHelper.GenerateUrl(以及使用它的ActionLink,RedirectToAction等)沒有可讓您對錨點進行編碼的魔術屬性名稱。

正如你指出的那樣,你可以推出自己的產品。這可能是最乾淨的解決方案。

Hackily,你可以只包括路由的錨和在你的參數哈希指定的值:

routes.MapRoute("WithTarget", "{controller}/{action}/{id}#{target}"); 
... 
<%= Html.ActionLink("Home", "Index", new { target = "foo" })%> 

這將產生像/首頁/索引/#FOO的URL。不幸的是,這對於顯示在URL末尾的URL參數並不適用。因此,只有在所有參數都顯示爲URL路徑組件的非常簡單的情況下,這種攻擊纔可行。

+1

不起作用。 #被轉換爲%23。 – jpbochi 2012-09-25 01:22:58

3

@Dominic,

我幾乎可以肯定,將在該航線將導致路由問題。

@Ricky,

直到MVC支持這個功能,你可以多一點「老同學」你如何讓你的路線。例如,你可以轉換:

<%= Html.ActionLink("Home", "Index") %> 

到:

<a href='<%= Url.Action("Index") %>#2345'>Home</a> 

或者你可以寫自己的幫手,基本上做同樣的事情。

+1

我嘗試了ASP.NET MVC Preview 3的路由目標,並且路由沒有問題。所以,儘管你「幾乎是積極的」,但對你來說將會很有幫助。 – 2009-05-19 03:54:43

18

正如Brad Wilson所寫,您可以通過簡單地連接字符串在視圖中建立自己的鏈接。然而,對於一個片段名追加到通過RedirectToAction(或類似)產生的重定向,你需要這樣的事:

public class RedirectToRouteResultEx : RedirectToRouteResult { 

    public RedirectToRouteResultEx(RouteValueDictionary values) 
     : base(values) { 
    } 

    public RedirectToRouteResultEx(string routeName, RouteValueDictionary values) 
     : base(routeName, values) { 
    } 

    public override void ExecuteResult(ControllerContext context) { 
     var destination = new StringBuilder(); 

     var helper = new UrlHelper(context.RequestContext); 
     destination.Append(helper.RouteUrl(RouteName, RouteValues)); 

     //Add href fragment if set 
     if (!string.IsNullOrEmpty(Fragment)) { 
      destination.AppendFormat("#{0}", Fragment); 
     } 

     context.HttpContext.Response.Redirect(destination.ToString(), false); 
    } 

    public string Fragment { get; set; } 
} 

public static class RedirectToRouteResultExtensions { 
    public static RedirectToRouteResultEx AddFragment(this RedirectToRouteResult result, string fragment) { 
     return new RedirectToRouteResultEx(result.RouteName, result.RouteValues) { 
      Fragment = fragment 
     }; 
    } 
} 

,然後在你的控制器,你會打電話:

return RedirectToAction("MyAction", "MyController") 
     .AddFragment("fragment-name"); 

這應該正確生成URL。

+3

這太好了。在MVC3中,「ActionLink」有一個重載,可以讓你做一個片段,但不是`RedirectToAction`。 – kamranicus 2011-03-18 04:30:50

+0

你,先生,是我的英雄。 :) – 2011-07-27 15:51:58

0

這是一個客戶端解決方案,但如果你有jQuery可用,你可以做這樣的事情。

<script language="javascript" type="text/javascript"> 
    $(function() { 
     $('div.imageHolder > a').each(function() { 
      $(this).attr('href', $(this).attr('href') + '#tab-works'); 
     }); 
    }); 
</script> 
4

在MVC3(甚至更早我沒有檢查),你可以使用UrlHelper.GenerateUrl傳遞片段參數。這裏有一個輔助方法,我用它來包裹functionalityL

public static string Action(this UrlHelper url, string actionName, string controllerName, string fragment, object routeValues) 
{ 
    return UrlHelper.GenerateUrl(
     routeName: null, 
     actionName: actionName, 
     controllerName: controllerName, 
     routeValues: new System.Web.Routing.RouteValueDictionary(routeValues), 
     fragment: fragment, 
     protocol: null, 
     hostName: null, 
     routeCollection: url.RouteCollection, 
     requestContext: url.RequestContext, 
     includeImplicitMvcValues: true /*helps fill in the nulls above*/ 
    ); 
} 
相關問題