3

How can I redirect to an ActionResult of a normal controller from .cshtml page of my Area's view?

I have a common login screen for normal/admin user. So based on the usertype json result will return the user type. So now I want to redirect a user based on the usertype.

If a user is normal user then I can redirect him using "var redirectUrl = '@Url.Action("Index", "Home")';" and then "window.location.href = redirectUrl;" Above code is working fine.

But what to do in following case? If a user is an admin, then I want to redirect him to "Index" ActionResult of "HomeController" which is present in "Admin" area.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104

3 Answers3

4

To redirect outside the area use a blank string for the area:

@Url.Action("Index", "Home", new { area = "" })

This will default you back to the HomeController outside of the area.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • @hutchnoid @NightOwl888 : Thanks Finally below syntax worked for me. `var adminHomePageUrl = '@Url.Action("Index","Home", new { area = "Admin" })';` – wasim mulla Aug 11 '15 at 14:55
1

If I understand correctly, you just need some conditional logic on your link.

@{
    var redirectUrl = Url.Action("Index", "Home", new { area = "" })

    if (this.User.IsInRole("Administrator"))
    {
        redirectUrl = Url.Action("Index", "Home", new { area = "Admin" })
    }
}

<script>
    var redirectUrl = '@redirectUrl';
    window.location.href = redirectUrl;
</script>

Although, a better alternative might be to make your own HTML helper extension method so you can reuse the logic elsewhere.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
1

Finally below syntax worked for me.

var adminHomePageUrl = '@Url.Action("Index","Home", new { area = "Admin" })';