@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>
}
I face this issue. For default route, there are more than one url for a given view. For example, home/index matched against when requesting,
I tried to use Url.Action() to check the equality. But the success or failure of the check depend on the url requested. i.e. When requesting "\" (app root) then check evaluate to true. When requesting "home\index" check evaluates to false. Url.Action() evaluates to "\".
Is there a more reliable way to check current view url with some mvc url with default route?
You should directly check the route data instead of the url, especially the "controller" and "action" values.
You get those using ViewContext.RouteData.Values["controller"]
and ViewContext.RouteData.Values["action"]
. Then, you can make sure controller is "Home" and action is "Index".