Asp.net mvc Url.RouteUrl返回null
我正在为一条路线建立一个UrlHelper 如 问题是返回的值总是空的 在中调试时发现 Url.RouteUrl(“x”)返回空值 Url.RouteCollection[“X”]返回路线 我正在努力做到:Asp.net mvc Url.RouteUrl返回null,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我正在为一条路线建立一个UrlHelper 如 问题是返回的值总是空的 在中调试时发现 Url.RouteUrl(“x”)返回空值 Url.RouteCollection[“X”]返回路线 我正在努力做到: public static string Category(this UrlHelper helper, int Id, string category) { return helper.RouteUrl("X", new {id = Id, category=
public static string Category(this UrlHelper helper, int Id, string category)
{
return helper.RouteUrl("X", new {id = Id, category= category});
}
我看不出我哪里做错了这似乎是因为注册路由时没有为{id}和{category}指定默认值
Url.RouteUrl(“x”)
将返回null,因为没有提供id和类别的值,并且您的路由定义没有默认值
我想你会发现,如果你更新你的路线条目,为id和类别指定一个默认值,这将解决你的问题。或者,如果您确定始终为id和category提供一个值,则可以不使用它
就您的实际Url帮助器方法Category()而言,如果您为id和Category提供一个非null或空值,那么它应该可以正常工作。我真的复制了代码,它对我很有用。出于某种原因,我仍然在运行mvc候选版本
我安装了mvc 1.0,现在它在你的Global.asax中运行良好,你真的注册了一个名为“X”的路由吗?是的,有一个路由注册为routes.MapRoute(“X”,“/category/{id}/{category}”,new{controller=“category”,action=“List”}),我已经设置了所有默认值,但仍然存在这个问题。