Url.RouteUrl返回null [英] Url.RouteUrl returns null

查看:77
本文介绍了Url.RouteUrl返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为路线构建UrlHelper 如最佳做法

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

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

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

Url.RouteUrl("x")返回null

Url.RouteUrl("x") return null

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

Url.RouteCollection["X"] return Route

我正在尝试:

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

推荐答案

这似乎是由于注册路由时未为{id}和{category}指定默认值引起的.

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")将返回null,因为没有提供id和category的值,并且您的路由定义没有默认值.

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.

我认为您会发现是否更新了路由条目以为ID和类别指定默认值,这将解决您的问题.另外,如果您确定总是 提供id和category的值,则可以不用它.

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.

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

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.

这篇关于Url.RouteUrl返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆