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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With