2012-03-02 19 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, “客户”)

相关问题