2014-02-19 54 views
0

我有點掙扎在ASP.NET MVC4中獲取動作url。Razor語法 - ActionLink不顯示正確的鏈接

我使用下面的剃刀語法創建鏈接:

@Html.ActionLink("some text", "Create", "Reports") 

輸出我得到的是:

<a href="/?action=Create&amp;controller=Reports">some text</a> 

我要的是:

<a href="/Reports/Create">some text</a> 

什麼想法,有什麼我失蹤了?

UPDATE:

RouteConfig是如下:

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

    // ignoring .aspx, .asmx files 
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); 
    routes.IgnoreRoute("{resource}.asmx/{*pathInfo}"); 

    routes.MapPageRoute("DefaultRoute", string.Empty, "~/Default.aspx"); 
    routes.MapPageRoute("MapsRoute", "Map", "~/Default.aspx"); 

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

月2日更新

,如果我的評論從以下途徑配置線,我得到的網址,如預期。任何想法爲什麼?

routes.MapPageRoute("DefaultRoute", string.Empty, "~/Default.aspx"); 
routes.MapPageRoute("MapsRoute", "Map", "~/Default.aspx"); 
+7

你能告訴我們你的路由是如何配置? – rla4

+0

由於rla4表明它可能是一個路線問題,我認爲這個問題是你的路線。你有'ActionLink'幫手的正確語法。 –

回答

0

試試這個

@Html.ActionLink("some text", "Create", "Reports",null,null) 

嘗試在route.config

routes.MapPageRoute("DefaultRoute", "{def}", "~/Default.aspx"); 
routes.MapPageRoute("MapsRoute", "{map}", "~/Default.aspx"); 

基類的actionlink如下補充:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    Object htmlAttributes 
) 
+0

ActionLink有很多重載..我有的語法是正確的..!無論如何,這是行不通的。 –

+0

@NarayanAkhade請檢查我的更新回答 – Amit

+0

它沒有工作!似乎有使用MapPageRoute的問題,它搞亂了ActionLinks?現在,我添加了routes.IgnoreRoute(「」);在路由中,所以默認頁面將重定向到Default.aspx。 –