Url.RouteUrl returns null

邮差的信 提交于 2019-12-08 17:00:39

问题


I'm buiding a UrlHelper for a route as in best practices

the problem is that the returned value is always null when debugging in found that

Url.RouteUrl("x") return null

Url.RouteCollection["X"] return Route

i'm trying to do :

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

I can't see where I'm doing something wrong


回答1:


It appears that this is being caused because you did not specify a default value for {id} and {category} when registering your routes.

Url.RouteUrl("x") will return null because there's no value for id and category provided, and your route definition does not have a default.

I think that you will find if you update your route entry to specify a default value for id and category this will solve your problem. Alternatively, if you are sure to always provide a value for id and category, you can do without it.

As far as your actual Url helper method Category(), that should be working just fine as-is if you are providing a non-null or empty value for id and category. I literally copied the code and it works for me.




回答2:


For some reason i was still running the mvc release candidate I installed the mvc 1.0 and now it works fine



来源:https://stackoverflow.com/questions/1278969/url-routeurl-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!