Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling return RedirectToAction("Activity") outside controller

I am building a fluent interface and would like to call the code below outside my controller...

return RedirectToAction("Activity");

How would I design this method? I have:

    public FluentCommand RedirectOnSuccess(string url)
    {
        if (IsSuccess)
            ;// make call here...

        return this;
    }

Note: IsSuccess is set here:

public FluentCommand Execute()
        {
            try
            {
                _command.Execute();
                IsSuccess = true;
            }
            catch (RulesException ex)
            {
                ex.CopyTo(_ms);
                IsSuccess = false;
            }
            return this;
        }

I call my fluent interface:

var fluent = new FluentCommand(new UpdateCommand(param,controller,modelstate)
.Execute()
.RedirectOnSucess("Actionname");
like image 230
Haroon Avatar asked Aug 11 '11 09:08

Haroon


People also ask

What is return RedirectToAction?

return RedirectToAction()To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View() method does. Browser receives this notification to redirect and makes a new request for the new action.

What is difference between redirect and RedirectToAction?

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.

How we can redirect to another page or controller?

Redirect() method The first method od redirecting from one URL to another is Redirect(). The Rediect() method is available to your controller from the ControllerBase class. It accepts a target URL where you would like to go.

How do I pass multiple parameters in RedirectToAction?

Second, to pass multiple parameters that the controller method expects, create a new instance of RouteValueDictionary and set the name/value pairs to pass to the method. Finally call RedirectToAction(), specifying the method name, controller name, and route values dictionary.


1 Answers

You could store an instance of the HttpContextBase as a field inside your fluent interface and when you need to redirect:

var rc = new RequestContext(context, new RouteData());
var urlHelper = new UrlHelper(rc);
context.Response.Redirect(urlHelper.Action(actionName), false);
like image 144
Darin Dimitrov Avatar answered Sep 29 '22 10:09

Darin Dimitrov