Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.RouteUrl returns null

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

like image 635
freddoo Avatar asked Aug 14 '09 16:08

freddoo


1 Answers

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.

like image 153
Kurt Schindler Avatar answered Sep 17 '22 08:09

Kurt Schindler