MVC 映射 - RedirectToAction 不起作用

发布于 2024-12-29 08:19:26 字数 755 浏览 1 评论 0原文

我有一个 MVC 项目,将默认映射更改为:

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}",
                new { controller = "Home", action = "Index", 
                      id = UrlParameter.Optional, arg = UrlParameter.Optional }
            );

以下控制器:

/Controllers/HomeController.cs:

public ActionResult Index(int? id)
{        
    return RedirectToAction("Index", "EPOS");
}

/Controllers/EPOSController.cs:

public ActionResult Index(int? id)
{
    return View();
}

正在命中断点在 HomeController 中但没有在 EPOSController 中命中,我收到此错误:

路由表中没有路由与提供的值匹配。

我做错了什么?

I have an MVC Project witch the default Mapping whcih I changed to:

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}",
                new { controller = "Home", action = "Index", 
                      id = UrlParameter.Optional, arg = UrlParameter.Optional }
            );

following Controllers:

/Controllers/HomeController.cs:

public ActionResult Index(int? id)
{        
    return RedirectToAction("Index", "EPOS");
}

/Controllers/EPOSController.cs:

public ActionResult Index(int? id)
{
    return View();
}

A breakpoint is being hit in the HomeController but isn't hit in the EPOSController, I get this error:

No route in the route table matches the supplied values.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2025-01-05 08:19:26

我相信问题是因为您的 Default 路线不正确:

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}", 
                new { controller = "Home", action = "Index",  
                      id = UrlParameter.Optional, arg = UrlParameter.Optional } 
            ); 

您不能在必需参数之前添加可选参数。如果将上面的路由改为这样会发生什么?

routes.MapRoute("Default", "{controller}/{action}/{id}/{arg}", 
                new { controller = "Home", action = "Index",  
                      id = UrlParameter.Optional, arg = UrlParameter.Optional } 
            ); 

I believe the problem is because your Default route isn't correct:

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}", 
                new { controller = "Home", action = "Index",  
                      id = UrlParameter.Optional, arg = UrlParameter.Optional } 
            ); 

You can't have an optional parameter preceeding a required parameter. What happens if you change the above route to this?

routes.MapRoute("Default", "{controller}/{action}/{id}/{arg}", 
                new { controller = "Home", action = "Index",  
                      id = UrlParameter.Optional, arg = UrlParameter.Optional } 
            ); 
杀お生予夺 2025-01-05 08:19:26

我认为 Shark 是对的,尝试将你的路线图分成两个像这样的定义(我还没有测试过这个)

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}",
            new { controller = "Home", action = "Index", 
                  arg = UrlParameter.Optional }
        );
routes.MapRoute("Default", "{controller}/{id}",
            new { controller = "Home", action = "Index", 
                  id = UrlParameter.Optional }
        );

I think Shark is right, try splitting your Route Map into two definitions something like this (I have not tested this)

routes.MapRoute("Default", "{controller}/{id}/{action}/{arg}",
            new { controller = "Home", action = "Index", 
                  arg = UrlParameter.Optional }
        );
routes.MapRoute("Default", "{controller}/{id}",
            new { controller = "Home", action = "Index", 
                  id = UrlParameter.Optional }
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文