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");
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With