c# - ASP.NET MVC RedirectToAction 未重定向到另一个 Controller 的索引

标签 c# asp.net-mvc redirecttoaction

在我的帐户/登录 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/

相关文章:

asp.net-mvc - IHubContext SignalR MVC 的 Autofac 注入(inject)

jquery - 在 asp.net mvc 中的 Post 操作后保留 jquery 选项卡位置

c# - RedirectToAction 生成错误的 url(查询字符串参数而不是路由数据参数)

.net - 在使用 ASP.NET MVC 的操作方法中使用哪种重定向方法进行 SEO? RedirectToAction 还是 RedirectToActionPermanent?

c# - 从 Entity Framework 中的集合加载相关实体

c# - 通过 C# 控制台应用程序运行批处理文件。命令被忽略

c# 在域驱动设计中放置业务规则

c# - TFS 构建输出中不包含链接的内容文件

C# 在方法参数内等待

ASP.Net Web API : Send response before request body is read/uploaded