asp.net mvc:将 RedirectToAction(string, object) 转换为 RedirectToAction(x => x.Detail(id))
任何人都知道如何创建一个方法(我将把它放在扩展类中),该方法将仅使用表达式(无魔术字符串)执行与 mvc 的 RedirectToAction 相同的操作。
因此,不要写这样的东西:
RedirectToAction("Detail",
new RouteValueDictionary { {"messageId", messageId}});
我会这样做:
this.RedirectToAction(x => x.Detail(messageId));
我尝试并做了类似的事情,但我不知道如何添加参数:
public static RedirectToRouteResult RedirectToAction<T>(this T controller,
Expression<Action<T>> expression)
{
return RedirectToAction(
(expression.Body as MethodCallExpression).Method.Name
);
}
anybody knows how to make a method (I will put it in a extensions class) that will do the same as the mvc's RedirectToAction only using expressions (no magic strings).
So that instead of writing something like this:
RedirectToAction("Detail",
new RouteValueDictionary { {"messageId", messageId}});
I would do like this:
this.RedirectToAction(x => x.Detail(messageId));
I tried and did something like this but I don't know how to add the parameters:
public static RedirectToRouteResult RedirectToAction<T>(this T controller,
Expression<Action<T>> expression)
{
return RedirectToAction(
(expression.Body as MethodCallExpression).Method.Name
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MvcContrib 有它: http://mvccontrib.codeplex.com/wikipage?title=RedirectToAction
MvcContrib has it: http://mvccontrib.codeplex.com/wikipage?title=RedirectToAction
http://www.clariusconsulting.net/blogs/ kzu/archive/2008/04/07/59274.aspx
另外,我相信他们在 1.0 发布时将类型安全的 HtmlHelper 扩展移至 MVC Futures 程序集中,但我现在似乎找不到它。
http://www.clariusconsulting.net/blogs/kzu/archive/2008/04/07/59274.aspx
Also, I believe they moved type-safe HtmlHelper extensions into the MVC Futures assembly back when 1.0 came out, however I can't seem to find it now.