1
votes

I'm using ASP.NET MVC in Visual Studio 2015. The app has the following structure:

  • MyApp

    • Controllers

      • Controller1
        • Actions
          • Create
          • Delete
          • Details
          • Edit
          • IndexPartial
      • Controller2
        • Actions
          • Edit
      • Controller3
        • Actions
          • Edit
    • Views

      • Controller1
        • Create
        • Delete
        • Details
        • Edit
        • IndexPartial
      • Controller2
        • Edit
      • Controller3
        • Edit

The app displays Controller1/IndexPartial view on the Controller2/Edit view and on Controller3/Edit. This partial view displays rows of data, each with Edit, Details, Delete buttons which take the user to the Controller1 views for those actions.

When the user is done with the Controller1 action, they need to return to Controller2/Edit or Controller3/Edit via the Back to List button or when the Save/Delete buttons are clicked. But how do we determine where the user originated? Did the user come from the Edit of Controller2 or Controller3?

We've thought of using a session variable. Can RouteConfig.cs be used to track the user's path and help determine where s/he should return? How do we do this via routes in MVC?

Thank you for your help.

Update: This is all done via the server; no JavaScript (Angular, etc.).

3
Don't use Session variables for things like this. If the session times out (for whatever reason and there are plenty) your form will break. Instead simply pass a Parameter in the URI indicating the return page.Krisztián Balla
@JennyO'Reilly, thanks. Would the parameter require a change to the routes? Do you mind expounding on this a bit in an answer with an example?Alex
I would not put this into the routes at all, because it makes those unnecessarily complex. You could simply pass a value (POST or GET) to the called endpoint (whatever route) and check for it in the Action method using the Request object. Maybe wrap this check in a helper object/method, so that you don't have to hardcode the name of the value each time.Krisztián Balla

3 Answers

3
votes

The routing engine has nothing to do with what you need. You need to track user navigation and a good way to do this is using ActionFilters.

You can create a custom ActionFilter that checks the UrlReferrer on its OnActionExecuted and decides how to redirect the request to the appropriate Controller/Action.

[Example]

ActionFilter

public class RedirectAfterActionFilter : ActionFilterAttribute, IActionFilter
{

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // Your decision logic
        if (filterContext.HttpContext.Request.UrlReferrer.AbsolutePath == "something usefull")
        {
            filterContext.Result = new RedirectToRouteResult("Your Route Name", routeValues: null); // redirect to Home
        }

        base.OnActionExecuted(filterContext);
    }
}

ActionFilter usage

    [RedirectAfterActionFilter]
    public ActionResult DoSomethingAndGetRedirected()
    {
        // Save, Edit or Whatever
        //...

        return new EmptyResult(); // no need to return since the user will be redirected by the filter
    }

Extra: Read How to redirect from a action filter if you dislike to use Route names to redirect.

3
votes

There are two aspects to this:

  • The "Back to List" link
  • The "Save/Delete" actions

As far as the "Back to List" link, your controller should be giving the view all the information it needs to produce a viable GUI. Pass an identifier (or even the actual return URL) to the view in the ViewBag as a dynamic property and let the view render the link to the destination.

For the "Save/Delete" actions, it depends on how they are implemented.

If it's all JS with http requests then the same concept above applies.

If you are posting back to the server however, the controller will have to do the redirection with something like RedirectToAction().

0
votes

How about storing the previous location in a ViewBag and then populate your button href with the ViewBag content...

Or

You can use Url Referrer, which fectches the previous url that linked to current page.

Of course the best method will depend on your implementantion, without seeing your code those two are the best option that I can think of.