在我的帐户/登录 Controller 方法中,我有类似的内容:
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 中的 Index 之外,所有情况在 switch block 中都工作正常。相反,它会将我带到 localhost:port/Registration,其中省略了操作 Index。
如果将 ActionName 更改为其他名称(例如 Index2),则效果很好。如果 Controller 名称更改为其他名称,也可以正常工作。
路由配置是 只是创建项目时自动生成的代码,如下:
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 }
);
}
提前致谢。
最佳答案
路由设置没有任何问题,它在 URL
中不包含 Index
的原因是根据默认路由
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
当您输入/Registration
时,路由已经知道默认操作是index
,因此它不会在中添加
和关于 /index
URL
403.14 forbidden
如果你看这篇文章 HTTP Error 403.14 - Forbidden - MVC 4 with IIS Express这可能是因为您的项目目录中可能有一个名为 Registration
的文件或文件夹
关于c# - ASP.NET MVC RedirectToAction 未重定向到另一个 Controller 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042918/