本文介绍了确实.NET MVC有一个强类型RedirectToAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
既然我已经决定让同时与测试呆现在RC走了,我不知道是否强类型 RedirectToAction
已添加的方式。有没有人尝试过了,有一个强类型 RedirectToAction
(也许 ActionLink的
)的RC?
Since I have decided to let RC go while staying with Beta for now, I have no way of knowing whether a strongly typed RedirectToAction
has been added. Has anybody tried it and is there a strongly typed RedirectToAction
(and maybe ActionLink
) in RC?
推荐答案
没有,没有。
protected RedirectToRouteResult RedirectToAction<T>(Expression<Action<T>> action, RouteValueDictionary values) where T : Controller
{
var body = action.Body as MethodCallExpression;
if (body == null)
{
throw new ArgumentException("Expression must be a method call.");
}
if (body.Object != action.Parameters[0])
{
throw new ArgumentException("Method call must target lambda argument.");
}
string actionName = body.Method.Name;
var attributes = body.Method.GetCustomAttributes(typeof(ActionNameAttribute), false);
if (attributes.Length > 0)
{
var actionNameAttr = (ActionNameAttribute)attributes[0];
actionName = actionNameAttr.Name;
}
string controllerName = typeof(T).Name;
if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
{
controllerName = controllerName.Remove(controllerName.Length - 10, 10);
}
RouteValueDictionary defaults = LinkBuilder.BuildParameterValuesFromExpression(body) ?? new RouteValueDictionary();
values = values ?? new RouteValueDictionary();
values.Add("controller", controllerName);
values.Add("action", actionName);
if (defaults != null)
{
foreach (var pair in defaults.Where(p => p.Value != null))
{
values.Add(pair.Key, pair.Value);
}
}
return new RedirectToRouteResult(values);
}
这应该工作。
这篇关于确实.NET MVC有一个强类型RedirectToAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!