MVC5(Asp.Net4.5.2)- RedirectToAction在一种情况下不返回

本文关键字:一种 情况下 返回 Net4 Asp MVC5 RedirectToAction | 更新日期: 2023-09-27 18:04:56

在我的帐户控制器中,在登录动作中,有以下代码:

                case "Sucess":
                    string rule = CheckRule(model.username, model.Password);
                    Response.SetCookie(SetAuthCookie(model.username, model.RememberMe, rule));
                    return RedirectToAction("Index", rule);

在checkrule中,i返回带有其他控制器名称的字符串,其中包括Admin和BasicUser,以下是它们的代码:

{
[Authorize]
public class AdminController : Controller
{
    private bool attAuthor = isAuthorized();
    private bool attAuth = isAuthenticated();
    private string rule = returnrule();
    // GET: Admin
    public ActionResult Index()
    {
        if (!attAuthor)
        {
            return RedirectToAction("erro401",rule);
        }
        else
        {
            return View();
        }

    }
    public ActionResult erro401()
    {
        return View("erro401");
    }

}

:

{
[Authorize]
public class BasicUserController : Controller
{
    private bool attAuthor = isAuthorized();
    private bool attAuth = isAuthenticated();
    private string rule = returnrule();
    // GET: BasicUser
    public ActionResult Index()
    {
         if (!attAuthor)
        {
            return RedirectToAction("erro401", rule);
        }
        else
        {
            FormsAuthenticationTicket authticket = get_ticket();
            string str = rule + " / " + authticket.Name;
            ViewBag.Htmlstr = str;
            return View();
        }

    }
    public ActionResult erro401()
    {
        return View("erro401");
    }

}

}

在RouteConfig中:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
           name: "Default",
           url: "",
           defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
       );
        routes.MapRoute(
            name: "BasicUser",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "BasicUser", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
             name: "Admin",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
         );

如果我用admin用户登录,它工作得很好,但是对于一个基本用户,navagador不会重定向到路由,只是登录屏幕,如果我输入控制器地址工作。我添加了一个标签html来查看cookie userdata,这很好(show de BasicUserRule)。

抱歉,如果我的问题不是很清楚,我是新手…

你的路由有两个问题:

  1. 我不建议将"映射到login。这实际上是主页。将主页映射到登录没有多大意义。当您从任何页面、属性返回HttpStatusCode.Unauthorized响应时,应该自动请求登录页面。如果您希望您的主页仅由授权用户访问,那么也从主页返回未经授权的响应。就这样吧。

    这是一个非常好的主意,阅读更多关于MVC(路由,控制器,身份验证,授权)是如何工作的。否则你可能会得到一个远不安全的应用。StackOverflow可以很好地解决单个问题,但不能帮助您看到全局。
  2. 当你有完全相同的两个模式彼此相邻时,只有第一个模式会被匹配,因为MVC在找到匹配时停止。您的默认值在这里不起作用。相反,您需要定义这样的模式:

    "Admin/{action}/{id}", new { controller = "Admin" } 
    "Basic/{action}/{id}", new { controller = "Basic" }
    

    所以,如果URL以"Basic"开头,Admin将不匹配,反之亦然。

你可以使用路由调试器来了解路由匹配是如何工作的,以及你的请求是如何映射到路由的。

MVC5(Asp.Net4.5.2)- RedirectToAction在一种情况下不返回

谢谢!

我的问题是路由,我设置了路由与控制器在url参数:

routes.MapRoute(
                name: "BasicUser",
                url: "BasicUser/{action}/",
                defaults: new { controller = "BasicUser", action = "Index" }
            );
            routes.MapRoute(
                 name: "Admin",
                 url: "Admin/{action}/",
                 defaults: new { controller = "Admin", action = "Index"}
             );

这解决了所有问题。我听从了他的建议,并寻找了更多的信息,这里有一些有趣的链接:

带有多个控制器的不同区域的MVC路由

Customizing-Routes-in-ASP-NET-MVC

(PT-BR) trabalhando-com-rotas-no-aspnet-mvc.aspx

我认为你的MapRoutes是不需要的,因为你只是重定向到一个视图。因为两个MapRoutes具有相同的结构,所以它将只保留最后一个Admin.

相关文章: