重定向到操作会保留路由参数对新操作无效
这是我的路线:
_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将以下规则替换
为
It Works。
在编写规则时,您应该知道只有最后一个参数可以是可选的,强制参数应该放在前面。替换它与规则(修改后的规则)不匹配,尽管它应该匹配。我没有确切的原因,但是当您传递第一个(消息[隐式)和第三个参数[动作]时,它也尝试匹配 {message} ,这是不合逻辑的。因此规则是在开头放置强制参数,如stackoverflow问题也是。
Replace the following rule
to
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.
我认为您需要切换“特定于消息的操作”路线和“常规消息操作”的顺序
I think you need to switch the order of your 'Message-specific Actions' route and 'General Message Actions'