重定向到操作会保留路由参数对新操作无效

发布于 2024-10-17 21:06:32 字数 1536 浏览 0 评论 0原文

这是我的路线:

_routes = RouteTable.Routes;

_routes.Clear();

_routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
_routes.IgnoreRoute("{*favicon}", new {favicon = @"(.*/)?favicon.ico(/.*)?"});

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{message}/{action}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

_routes.MapRoute(
    "General Message Actions",
    "Messages/{action}",
    new {controller = "Messages", action = "Index"},
    new {action = @"\D+"}
    );

_routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    );

_routes.MapRoute(
    "Catch All",
    "{*path}",
    new {controller = "Error", action = "Error"}
    );

这是我的控制器的相关部分:

public class MessagesController
{
    public ActionResult Index()
    {
        return View();
    }

    // GET: ~/Messages/1
    public ActionResult ViewMessage(int message)
    {
        return View(// stuff to get message from repo);
    }

    [HttpPost]
    // POST : ~/Messages/1/Delete
    public ActionResult Delete(int message)
    {
        // do stuff
        return RedirectToAction("Index");
    }
}

问题是,在 Delete 中重定向后浏览器中的 URL 不是我所期望的 ~/Messages/ ,而是(假设 message 是 12)~/Messages/12/Index

Index 甚至不接受 message 参数。我不明白为什么会发生这种情况。我需要改变什么?

Here are my routes:

_routes = RouteTable.Routes;

_routes.Clear();

_routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
_routes.IgnoreRoute("{*favicon}", new {favicon = @"(.*/)?favicon.ico(/.*)?"});

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{message}/{action}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

_routes.MapRoute(
    "General Message Actions",
    "Messages/{action}",
    new {controller = "Messages", action = "Index"},
    new {action = @"\D+"}
    );

_routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    );

_routes.MapRoute(
    "Catch All",
    "{*path}",
    new {controller = "Error", action = "Error"}
    );

Here's the relevant parts of my controller:

public class MessagesController
{
    public ActionResult Index()
    {
        return View();
    }

    // GET: ~/Messages/1
    public ActionResult ViewMessage(int message)
    {
        return View(// stuff to get message from repo);
    }

    [HttpPost]
    // POST : ~/Messages/1/Delete
    public ActionResult Delete(int message)
    {
        // do stuff
        return RedirectToAction("Index");
    }
}

Problem is, the URL in the browser after the redirect in Delete is not ~/Messages/ as I would expect, but instead it's (assuming message was, say, 12) ~/Messages/12/Index.

Index doesn't even accept a message parameter. I don't understand why this is happening. What do I need to change?

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

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

发布评论

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

评论(2

野侃 2024-10-24 21:06:33

将以下规则替换

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{message}/{action}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{action}/{message}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

It Works。

在编写规则时,您应该知道只有最后一个参数可以是可选的,强制参数应该放在前面。替换它与规则(修改后的规则)不匹配,尽管它应该匹配。我没有确切的原因,但是当您传递第一个(消息[隐式)和第三个参数[动作]时,它也尝试匹配 {message} ,这是不合逻辑的。因此规则是在开头放置强制参数,如stackoverflow问题也是。

Replace the following rule

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{message}/{action}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

to

_routes.MapRoute(
    "Message-specific Actions",
    "Messages/{action}/{message}",
    new {controller = "Messages", action = "ViewMessage"},
    new {message = @"\d+"}
    );

It works.

while writing rules, you should know that only the last parameter can be optional and the mandatory parameter should be placed before. Replacing this does not match rule(the modified one), although it should. I have no exact reason, but when you passed the 1st(Messages[implicitly) and 3rd parameter[action], it tried to match {message} too, which is not logical. so the rule is place mandatory params in the beginning as discussed in this stackoverflow question too.

梦回梦里 2024-10-24 21:06:33

我认为您需要切换“特定于消息的操作”路线和“常规消息操作”的顺序

I think you need to switch the order of your 'Message-specific Actions' route and 'General Message Actions'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文