url.Action(controller, action)无法按预期路由

4
在我的ASP.NET MVC 3中,我正在使用以下代码:
<a href=@url.Action("myController", "myaction")>

但是,当我点击它时,它并没有跳转到我的动作上。相反,在URL中我看到了这个:
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”对象的字符串参数时,通常会发生这种情况。 - Andras Zoltan
4个回答

11

第一个参数被解释为Action。

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

4
Url.Action 的签名如下所示:
Url.Action(string actionName, string controllerName)

根据您的代码,参数顺序不正确,您应该尝试如下:
@Url.Action("myAction", "myController")

也要记住,要移除控制器中的“Controller”部分。例如,如果我有一个名为CustomerController的控制器和一个Index操作,那么应该是这样的:
@Url.Action("Index", "Customer")

是的,你说得对 - 但我不确定我们所得到的代码是否准确,因为string,string重载仍会生成没有查询字符串的URL。 - Andras Zoltan

1
我遇到了同样的问题......我的解决方案是:

@Html.ActionLink("链接文本", "我的操作", "我的控制器", new { id=item.ID }, null)

只需在最后一个参数中添加null。

0

对于默认操作(在路由中未定义的操作),普通的HTTP GET请求会自动转到,您只需要使用null操作。

@Url.Action(null, "Customer")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接