重定向到行动基于角色不工作。也许需要路由选择? [英] Redirect to action based on role not working. Maybe routing needed?

查看:125
本文介绍了重定向到行动基于角色不工作。也许需要路由选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器

public class GlobalAdminController : Controller
{
    // GET: GlobalAdmin
    [AuthorizeUser(Roles = "admin")]
    public ActionResult Index()
    {
        return View();
    }
}

和家用控制器,它的主要目标网页应用

And the home controller which is the main landing page for the app

public ActionResult Index()
{
    if(User.IsInRole("admin"))
    {
        RedirectToAction("Index", "GlobalAdmin");
    }
    return View();
}

重定向到动作在调试器执行
然而,指数动作本身不被对全球管理执行

The redirect to action is executed in the debugger However the index action itself is not being executed on the global admin

我不知道是否有更好的方法来做到这一点?也许throught路由?

I wonder if there is a better way to do this? maybe throught routing?

推荐答案

您必须使用收益来提供重定向信息(网​​址)的浏览器。和浏览器重定向到新的位置。

You have to use return to provide the redirect informations (url) to the browser. And the browser will redirect to the new location.

public ActionResult Index()
{
    if(User.IsInRole("admin"))
    {
       return RedirectToAction("Index", "GlobalAdmin");
    }
    return View();
}


路由的一点是要提供一种方法来从控制器访问的操作。相反accesing的 GlobalAdmin /索引使用路径来提供像另一种方式管理​​/指数


The point of the routes is to provide a way to access the actions from controller. Instead of accesing GlobalAdmin/Index you use the route to provide another way like admin/index

routes.MapRoute( 
     name: "Default", 
     url: "admin/{action}/{id}", 
     defaults: new { controller = "GlobalAdmin", action = "Index", id = UrlParameter.Optional } ); 

routes.MapRoute( 
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

这篇关于重定向到行动基于角色不工作。也许需要路由选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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