简体   繁体   English

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

[英]MVC5(Asp.Net4.5.2)- RedirectToAction does'nt return in one case

in my Account controller, in login action, have the code below: 在我的帐户控制器中,在登录操作中,输入以下代码:

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

In checkrule, i return string with names of another controllers, among them are Admin and BasicUser, here are the codes of these: 在checkrule中,我返回带有其他控制器名称的字符串,其中包括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");
    }

} }

and: 和:

{
[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");
    }


}

} }

In the RouteConfig: 在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 }
         );

If I log in with an admin user it works perfectly, but with a basic user the navagador does not redirect to the route, simply is the logon screen, if I enter the controller address works. 如果我以管理员用户身份登录,则可以正常运行,但是对于基本用户,navagador不会重定向到该路由,如果我输入了控制器地址,则登录屏幕就是登录屏幕。 I added a tag html to view the cookie userdata and this works fine(show de BasicUserRule). 我添加了一个标签html来查看cookie用户数据,并且效果很好(显示de BasicUserRule)。

Sorry if my question is not very clear, i am newbie... 抱歉,如果我的问题不是很清楚,我是新手...

There are two problems with your routes: 您的路线有两个问题:

  1. I don't suggest mapping "" to login. 我不建议将“”映射到登录名。 That is actually the homepage. 那实际上是主页。 Mapping homepage to login doesn't make much sense. 将主页映射到登录没有多大意义。 Your login page should be requested automatically when you return an HttpStatusCode.Unauthorized response from any page, attribute. 返回HttpStatusCode.Unauthorized时,将自动请求登录页面。来自任何页面属性的HttpStatusCode.Unauthorized授权的响应。 If you want your homepage only accessed by authorized users, return Unauthorized response from Homepage too. 如果您只希望授权用户访问您的主页,请从“主页”返回“未经授权的响应”。 That would be it. 就是这样。

    It's a very good idea read more about how MVC (routing, controllers, authentication, authorization) works. 阅读有关MVC(路由,控制器,身份验证,授权)如何工作的更多信息是一个好主意。 Otherwise you might end up with an app far from secure. 否则,您最终可能会获得一个远离安全性的应用程序。 StackOverflow is good about solving individual issues but doesn't help you see the big picture. StackOverflow可以很好地解决单个问题,但并不能帮助您看到全局。 You still need to understand how stuff works. 您仍然需要了解东西是如何工作的。

  2. When you have exact same two patterns next to each other, only the first would be matched because MVC stops when it finds a match. 当两个完全相同的模式彼此相邻时,只有第一个模式会被匹配,因为MVC在找到匹配项时会停止。 Your default values don't help there. 您的默认值对您无济于事。 Instead, you need to define patterns like: 相反,您需要定义以下模式:

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

    so, if the URL starts with "Basic", Admin wouldn't be matched or vice versa. 因此,如果网址以“基本”开头,则不会匹配管理员,反之亦然。

You can use Route Debugger to understand how route matching works and how your requests get mapped to routes. 您可以使用Route Debugger来了解路由匹配的工作方式以及如何将您的请求映射到路由。

Thanks Sedat! 感谢Sedat!

My troubles were the routes, i set de route with controller in url parameter: 我的麻烦是路线,我在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"}
             );

This solved everything. 这解决了一切。 I followed his advice, and sought a little more information, here are some interesting links: 我遵循了他的建议,并寻求了更多信息,这是一些有趣的链接:

MVC Routing with different Areas with multiple controllers 具有多个控制器的不同区域的MVC路由

Customizing-Routes-in-ASP-NET-MVC 自定义的路由功能于ASP-NET-MVC

(PT-BR)trabalhando-com-rotas-no-aspnet-mvc.aspx (PT-BR)trabalhando-COM-ROTAS-NO-ASPNET,mvc.aspx

I think that your MapRoutes are not needed because you te simply redirecting to a view. 我认为不需要MapRoutes,因为您只需重定向到视图即可。 Because both MapRoutes hace the same structure, it will keep only the last one which is Admin. 由于两个MapRoute具有相同的结构,因此它将仅保留最后一个即Admin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM