Kye
Kye

Reputation: 6239

How do I handle invalid asp MVC routes

I have a number of MVC views that only make sense in the context of a workflow. E.g. user submits information on another page and is then redirected to a page which only display "success"..

Since the redirect is a GET, there is no way to stop users manually navigating to the page by adjusting their URL.

What should I do in this situation? Redirect to an error page, show a blank page?

Upvotes: 0

Views: 189

Answers (1)

shakib
shakib

Reputation: 5469

In my opinion sucess information views should not have routes, as they are not intended to be served from a browser url. in our projects we usually return the success view directly from an post action.for example,

 [HttpPost]
    public ActionResult ChangePassword(ChangePasswordModel model)
    {
        if (ModelState.IsValid)
        {

            bool changePasswordSucceeded;
            try
            {
                MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);
                changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
            }
            catch (Exception)
            {
                changePasswordSucceeded = false;
            }

            if (changePasswordSucceeded)
            {
                var infoModel = new AnyInfoModel();
                return View("ChangePasswordSuccess", infoModel); // This is not redirected, but success shown in the current route url.
            }
            else
            {
                ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
            }
        }
        return View(model);
    }

But this has a drawback also, if an user accidentally hit he F5 button the browser, it will try to repost the form.

Upvotes: 1

Related Questions