Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET MVC have a strongly typed RedirectToAction?

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?

like image 828
User Avatar asked Feb 08 '09 16:02

User


People also ask

What is the strongly typed view in MVC?

Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure. By specifying the type of data, you get access to IntelliSense for the model class.

What is RedirectToAction MVC?

RedirectToAction(String, Object) Redirects to the specified action using the action name and route values. RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

What is strongly typed view in MVC 5?

In ASP.NET MVC, we can pass the data from the controller action method to a view in many different ways like ViewBag, ViewData, TempData and strongly typed model object. If we pass the data to a View using ViewBag, TempData, or ViewData, then that view becomes a loosely typed view.

What is the difference between redirect and RedirectToAction in MVC?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


2 Answers

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.

like image 77
Darrell Mozingo Avatar answered Sep 20 '22 03:09

Darrell Mozingo


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.

like image 40
Chad Moran Avatar answered Sep 23 '22 03:09

Chad Moran