Does .NET MVC have a strongly typed RedirectToAction?
Asked Answered
K

4

19

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?

Kopans answered 8/2, 2009 at 16:32 Comment(1)
You should change the accepted answer to @Darrell Mozingo's answer; because RedirectToAction<T> is indeed in the Futures assembly.Indigenous
P
18

No, it doesn't.

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

That should work.

Paulson answered 8/2, 2009 at 16:36 Comment(6)
Do this exist in the Futures dll? i can't find it, if it does? I also wonder why it was missed??Antilepton
Chad .. how can this become an extension method to the Controller class?Antilepton
I'd recommend putting it on a base Controller class and making your controllers inherit from that.Paulson
The entire MVC framework should be strongly typed like this, I wish they would ditch magic strings, its so "DataSets". Thanx for the code Chad.Chukker
There is a dependency on a 'LinkBuilder' class, is this bespoke code?Liaotung
What's LinkBuilder?Gat
R
25

This is also included in MVC Contrib as an extension method on your controller, along with a lot of other strongly typed goodies for ModelState handling, testing, etc. It's well worth taking on the extra dependency for what it offers.

Regine answered 1/12, 2009 at 16:26 Comment(0)
P
18

No, it doesn't.

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

That should work.

Paulson answered 8/2, 2009 at 16:36 Comment(6)
Do this exist in the Futures dll? i can't find it, if it does? I also wonder why it was missed??Antilepton
Chad .. how can this become an extension method to the Controller class?Antilepton
I'd recommend putting it on a base Controller class and making your controllers inherit from that.Paulson
The entire MVC framework should be strongly typed like this, I wish they would ditch magic strings, its so "DataSets". Thanx for the code Chad.Chukker
There is a dependency on a 'LinkBuilder' class, is this bespoke code?Liaotung
What's LinkBuilder?Gat
C
5

You can use return RedirectToAction(nameof(Index));

Cicelycicenia answered 4/3, 2017 at 9:47 Comment(0)
S
0

If you don't want the full MvcContrib library, you can get just this feature by using the MvcNavigationHelpers NuGet package.

Suctorial answered 4/3, 2017 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.