【问题标题】:Url.RouteUrl returns nullUrl.RouteUrl 返回 null
【发布时间】:2009-08-14 16:57:45
【问题描述】:

我正在为路线构建 UrlHelper 如best practices

问题是返回值始终为null 在调试的时候发现

Url.RouteUrl("x") 返回 null

Url.RouteCollection["X"] 返回路由

我正在努力:

public static string Category(this UrlHelper helper, int Id, string category)
{
     return helper.RouteUrl("X", new {id = Id, category= category});
}

我看不出哪里做错了

【问题讨论】:

  • 在您的 Global.asax 中,您实际上是在注册名为“X”的路线吗?
  • 是的,有一条路由注册为 routes.MapRoute("X", "/category/{id}/{category}", new {controller="category", action="List"} )

标签: asp.net-mvc asp.net-mvc-routing


【解决方案1】:

这似乎是因为您在注册路线时没有为 {id} 和 {category} 指定默认值。

Url.RouteUrl("x") 将返回 null,因为没有提供 id 和 category 的值,并且您的路由定义没有默认值。

我认为您会发现,如果您更新路由条目以指定 id 和 category 的默认值,这将解决您的问题。或者,如果您确定总是为 id 和 category 提供一个值,则可以不使用它。

就您的实际 Url 帮助器方法 Category() 而言,如果您为 id 和类别提供非 null 或空值,应该可以正常工作。我从字面上复制了代码,它对我有用。

【讨论】:

  • 我已经设置了所有的默认值,但我仍然遇到这个问题。
【解决方案2】:

由于某种原因,我仍在运行 mvc 候选版本 我安装了 mvc 1.0,现在可以正常工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多