ASP.NET MVC RedirectToAction 没有重定向到另一个控制器的索引
ASP.NET MVC RedirectToAction not redirecting to Index of another controller
在我的 Account/Login 控制器方法中,我有类似的东西:
var classA = GetObject(); //actual code omitted
switch(classA.PropA)
{
case 1:
return RedirectToAction("Action2", "Registration");
//more case code omitted
default:
return RedirectToAction("Index", "Registration");
}
除默认情况外,所有情况在开关块中都可以正常工作,默认情况下它应该转到 RegistrationController 中的索引。相反,它将我带到本地主机:port/Registration,其中省略了操作索引。
如果将 ActionName 更改为其他内容(例如 Index2),则效果很好。如果将控制器名称更改为其他名称,也可以正常工作。
路由配置是
只是创建项目时自动生成的代码,如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
提前致谢。
尝试使用
return RedirectToAction("Index");
并且在 RouteConfig 中,您可以将 Action "Index" 路由到 Controller "Registration"
喜欢
routes.MapRoute("Index", "Index", new { controller = "Registration", action = "Index" });
如果您使用 RedirectionToAction("Index","ControllerName");
它将使用默认映射配置将您重定向到 localhost:port/ControllerName
在你的情况下,如果你执行 RedirectionToAction("Index","ControllerName");
它会将你重定向到
localhost:port/Registration
路由设置没有问题,因为根据 default route
它不在 URL
中包含 Index
的原因
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
当您输入 /Registration
时,路由已经知道默认操作 index
因此它不会在 URL
和关于
中添加 /index
403.14 forbidden
如果您查看此 post ,可能是因为您可能在项目目录
中有一个名为 Registration
的文件或文件夹
在我的 Account/Login 控制器方法中,我有类似的东西:
var classA = GetObject(); //actual code omitted
switch(classA.PropA)
{
case 1:
return RedirectToAction("Action2", "Registration");
//more case code omitted
default:
return RedirectToAction("Index", "Registration");
}
除默认情况外,所有情况在开关块中都可以正常工作,默认情况下它应该转到 RegistrationController 中的索引。相反,它将我带到本地主机:port/Registration,其中省略了操作索引。
如果将 ActionName 更改为其他内容(例如 Index2),则效果很好。如果将控制器名称更改为其他名称,也可以正常工作。
路由配置是 只是创建项目时自动生成的代码,如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
提前致谢。
尝试使用
return RedirectToAction("Index");
并且在 RouteConfig 中,您可以将 Action "Index" 路由到 Controller "Registration"
喜欢
routes.MapRoute("Index", "Index", new { controller = "Registration", action = "Index" });
如果您使用 RedirectionToAction("Index","ControllerName");
它将使用默认映射配置将您重定向到 localhost:port/ControllerName
在你的情况下,如果你执行 RedirectionToAction("Index","ControllerName");
它会将你重定向到
localhost:port/Registration
路由设置没有问题,因为根据 default route
URL
中包含 Index
的原因
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
当您输入 /Registration
时,路由已经知道默认操作 index
因此它不会在 URL
和关于
/index
403.14 forbidden
如果您查看此 post ,可能是因为您可能在项目目录
中有一个名为Registration
的文件或文件夹