简体   繁体   English

asp.net mvc:将RedirectToAction(string,object)变为RedirectToAction <Controller>(x => x.Detail(id))

[英]asp.net mvc: make RedirectToAction(string, object) into RedirectToAction<Controller>(x => x.Detail(id))

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). 任何人都知道如何创建一个方法(我将它放在扩展类中),它将使用mvc的RedirectToAction只使用表达式(没有魔术字符串)。

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
     );
 }

MvcContrib拥有它: http ://mvccontrib.codeplex.com/wikipage?title = RedirectToAction

http://www.clariusconsulting.net/blogs/kzu/archive/2008/04/07/59274.aspx 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. 此外,我相信当1.0出现时,他们将类型安全的HtmlHelper扩展转移到了MVC Futures组件中,但我现在似乎无法找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM