C# routes.MapRoute-此网页具有重定向循环

C# routes.MapRoute-此网页具有重定向循环,c#,asp.net-mvc,angularjs,asp.net-mvc-4,redirect,C#,Asp.net Mvc,Angularjs,Asp.net Mvc 4,Redirect,RouteConfig.cs routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Login", url: "login", defaults: new { controller = "Login", action = "Index" } ); routes.MapRoute( name: "Server", url: "server/{controller

RouteConfig.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Login",
    url: "login",
    defaults: new { controller = "Login", action = "Index" }
);

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

routes.MapRoute(
     name: "Client", 
     url: "{*url}",
     defaults: new { controller = "Home", action = "Index" }
);
if (!UserNotLoggedIn)
{
    return RedirectToAction("Login", "Index");
}
HomeController.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Login",
    url: "login",
    defaults: new { controller = "Login", action = "Index" }
);

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

routes.MapRoute(
     name: "Client", 
     url: "{*url}",
     defaults: new { controller = "Home", action = "Index" }
);
if (!UserNotLoggedIn)
{
    return RedirectToAction("Login", "Index");
}
逻辑:所有请求都转到
Home/Index
(我使用angular进行路由)
HomeController
中,我检查用户是否已登录。如果是,我将返回一个视图。如果没有,我想将用户重定向到登录。


问题是,当用户未登录时,他会被重定向到mydomain/login,但会得到“此网页具有重定向循环”。当我使用调试器运行时,我看到即使url是
/login
控制器和操作仍然是
主/索引
您得到的
重定向到操作()
参数顺序错误。首先是操作名称,然后是控制器:

protected internal RedirectToRouteResult RedirectToAction(string actionName, string controllerName);
就你而言:

return RedirectToAction("Index", "Login");
您当前的
RedirectToAction
调用可能会返回一个空字符串,这会导致浏览器再次“重定向”到同一页面,再次获得相同的重定向结果,最终导致重定向循环

此外,您最好使用
授权
过滤器,而不是执行自己的身份验证和重定向机制


请参见

我认为问题在于路线定义

routes.MapRoute(
    name: "Login",
    url: "login",
    defaults: new { controller = "Login", action = "Index" }
此处预期的url与登录/索引不匹配

不幸的是,匹配的url是

routes.MapRoute(
    name: "Client", 
    url: "{*url}",
    defaults: new { controller = "Home", action = "Index" }
这使它返回到主/索引。尝试更改“登录”的url格式,并传递与之匹配的url,您的问题应该会得到解决。

使用固定URL时,您应该小心。

您的问题可能是需要登录才能查看登录页面,请确保您的登录页面在控制器或方法中没有“授权”属性。

尝试添加带有返回URL的MapRoute。我喜欢在注册和登录时将用户重定向到请求的URL

         routes.MapRoute(
         null,
         "{controller}/{id}",
         new { controller = "Account", action = "Login" },
         new { returnUrl = @"regex for URLs" });

我试图替换为“ReturnRedirectToAction(“Index”,“Login”);”,但仍然得到“此网页具有重定向循环”是否已注册任何全局筛选器?您是否尝试使用
[AllowAnonymous]
属性来修饰
登录/索引
操作?我没有使用MVC身份验证,因此[AllowAnonymous]无论如何都不起作用。在FilterConfig.cs中只有“filters.Add(new HandleErrorAttribute());”(visual studio在项目创建中创建的通用文件)路由是否完全按照粘贴顺序定义?可能是
客户端
路由(使用
“{*url}”
模式)正在接管
/Login
请求。试着暂时将其注释掉。在这种情况下,我得到HTTP错误500.0-内部服务器错误为什么不使用MVC身份验证?如果需要对其进行自定义,只需将[
Authorize
]属性子类化即可。至少,您应该使用
IAuthorizationFilter
实现来执行此检查,而不是将其放入控制器操作中。