【发布时间】:2017-10-27 07:19:25
【问题描述】:
@if (Url.Action() == Url.Action("index", "home")) {
<text>
//some js code conditionally rendered.
alert('test'); //Only should output, if current urls is home/index
</text>
}
我面临这个问题。对于默认路由,给定视图有多个 url。 例如请求时匹配的 home/index,
- / - 从默认值派生的控制器和动作。
- 主页/索引
我尝试使用 Url.Action() 来检查相等性。但检查的成功或失败取决于请求的 url。即当请求“\”(应用程序根)时,然后检查评估为真。当请求“home\index”时,检查结果为假。 Url.Action() 计算结果为“\”。
有没有更可靠的方法来检查当前视图 url 与一些带有默认路由的 mvc url?
【问题讨论】:
-
见Test if a request's URL is in the Route table。测试 URL 是否相等是不可靠的。相反,您应该执行生成 URL 或测试传入路由的路由业务逻辑,以查看您是否收到非空响应。
标签: asp.net-mvc asp.net-routing