0
votes

I'm getting this error in an MVC app:

The view 'Index' or its master was not found. The following locations were searched:

~/Views/User/Index.aspx

~/Views/User/Index.ascx

~/Views/Shared/Index.aspx

~/Views/Shared/Index.ascx

I don't understand why it's coming up; I'm not trying to navigate to an Index view; I'm trying to navigate to a ChangeExpiredPassword route. The code for the ChangeExpiredPassword controller action is executing, but as soon as control passes out of that method, this error is thrown from the internals of MVC. I have no idea why we're trying to redirect to Index all of a sudden. I don't know what code would be relevant to share but I can add code upon request. Thanks!

edit: here is how we are getting to ChangeExpiredPassword, this is from a UserController class:

[HttpPost]
public ActionResult Index(LogOnModel model, string returnUrl)
{
    ...
    if (pwExpirationDate < DateTime.Now)
    {
        return ChangeExpiredPassword();
    }
    ...
}

and here is the ChangeExpiredPassword itself, minus some HTML building code:

[HttpGet]
public ActionResult ChangeExpiredPassword()
{
    ActionResult actionResult = null;

    try
    {
        ContentViewModel viewModel = new ContentViewModel();

        Form form = new Form();
        form.Method = FormMethod.Post;
        form.Action = "User/ChangeExpiredPassword";

        // this is where we build some HTML for the Change Expired Password view

        viewModel.ContentControls.Add(form);

        actionResult = View(viewModel);
    }
    catch (Exception ex)
    {
        HandleException(ex);
        actionResult = Error();
    }

    return actionResult;
}
1
What does the controller class and this particular action method do? What route is being requested? If this is an Index action and it's returning a view then, well, it'll look for an Index view.David
Oh, we are on an Index action actually - it's just redirecting to ChangeExpiredPassword if the user's password is expired. I'll add some relevant code so you can see.ekolis
What is ChangeExpiredPassword()? What does it return? It sounds like it's returning a View(). So I guess the question becomes... What do you want to happen at the end of this Index action? What should be returned to the user?David
HttpGet, HttpPost... this is fishy, if you want to redirect, use RedirectToActionStefan

1 Answers

2
votes

If ChangeExpiredPassword is itself an action, and if that action ends with this:

return View();

Then returning ChangeExpiredPassword() tries to return a view. However, the view chosen by the framework is based on the route being requested, not the method being called. From the framework's perspective, you're returning View() from within Index.

It sounds like you want to redirect the user to ChangeExpiredPassword. Which in your Index action would look more like this:

return RedirectToAction(nameof(ChangeExpiredPassword));

This will instruct the browser to initiate a new GET request for the ChangeExpiredPassword action. Which, semantically, is what you want to happen. There's no need to try to get clever with returning highly custom responses. Keep each request/response simple and RESTful. The response from Index is instructing the client "you need to go to ChangeExpiredPassword".