2012-03-02 50 views
2

在我的ASP.NET MVC 3不是路由,我使用此代碼url.Action(控制器,動作)如預期

<a [email protected]("myController", "myaction")> 

但是,當我點擊它,它簡化版,去我的行動。相反,在我看到這個網址

http://localhost:1402/?Length=2 

我錯過了什麼嗎?

謝謝。

編輯:

這裏是我的路線:

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

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 
+1

請包括Global.asax.cs中您的路線 - 你可以確認上述Url.Action呼叫* *完全相同一樣?這通常發生在一個字符串被作爲參數傳遞的時候,這個參數被認爲是一個'routeValues'對象 – 2012-03-02 14:51:40

回答

8

第一個參數interpretted作爲行動

@url.Action("myaction","myController") 
+0

我確信它是相反的,謝謝。 – kbaccouche 2012-03-02 15:24:15

2

Url.Action的簽名是:

Url.Action(string actionName, string controllerName) 

根據你的代碼,你的參數的順序是不正確的,你應該嘗試:

@Url.Action("myAction", "myController") 

還記得刪除控制器的「控制器」部分,對於爲例,如果我有一個指數作用的CustomerController這將是這樣的:

@Url.Action("Index", "Customer") 
+0

是的你是對的 - 但我不確定我們給出的代碼是否準確,因爲'string,string '重載仍然會呈現沒有查詢字符串的Url。 – 2012-03-02 14:59:35

1

我有同樣的問題... 我的解決辦法:

@Html.ActionLink("Link text", "myAction", "myController", new { id=item.ID }, null)

只需將null添加到最後一個參數。

0

對於默認操作(未在路由中定義的操作),正常HTTP GET將自動轉到,您只需使用null操作。

@ Url.Action(NULL, 「客戶」)

相關問題