RedirectToAction 有哪些替代方案?
在我的 HomeController 上,我有一个索引操作,它通过 RedirectToAction 方法重定向到 CustomerController 的索引操作。在本例中,我不喜欢 RedirectToAction
修改显示 mysite.com/Customer
的 URL。是否有一些替代机制来执行重定向,使我仍然可以利用 CustomerController
中的索引操作?
public RedirectToRouteResult Index()
{
return RedirectToAction("Index", "Customer");
}
On my HomeController
I have an index action which redirects to the index action of the CustomerController
via the RedirectToAction
method. In this instance I don't like how RedirectToAction
modifies the URL showing mysite.com/Customer
. Are there some alternative mechanisms to performing a redirection that will allow me to still leverage the index action from the CustomerController
?
public RedirectToRouteResult Index()
{
return RedirectToAction("Index", "Customer");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,RedirectToAction 使用路由表上的反向查找来生成其 URL。因此,如果您告诉路由模块您想要将“/Customer/Index”映射到控制器 Customer 和 Action Index,则 RedirectToAction 将获取您的 Index 和 Customer 参数并将它们转换为“/Customer/Index”。
因此,如果您想更改 RedirectToAction 的行为,您只需更改路由规则即可。
As I understood it, RedirectToAction uses a reverse lookup on the routing table to generate its URL. So if you've told the Routing module that you want to map "/Customer/Index" to Controller Customer and Action Index then RedirectToAction will take your Index and Customer arguments and convert them to "/Customer/Index".
So if you want to change the behaviour of RedirectToAction you simply need to change your routing rules.