0

Here's my issue. After the user logs in, I'll try to validate if a user password is temporal. If it is, I want to redirect to a different view. If not, I want to go to the app menu. However, when it redirects, it says that the resource is not found.

if (Session["IdUsuario"] == null)
{
    Response.Redirect("~/");
}
else
{
    if ((bool)Session["Temporal"] == true)
    {
        Response.Redirect("~/Login/ContraseñaTemporal.cshtml");// <--- Here is my issue
    }
kingdaro
  • 11,528
  • 3
  • 34
  • 38
Torake
  • 11
  • 1
  • 6
  • Why using `Response.Redirect`? If you're using MVC, use `RedirectToAction` to redirect based on controller and action name that returns view name instead of directly call CSHTML file path. `Response.Redirect` is typical webforms way to redirect into aspx pages, but in MVC redirection based on controller & action name parameters. – Tetsuya Yamamoto Apr 20 '18 at 02:58
  • im using redirect because im on a view , and it doesnt let me use redirect to action, sorry if my english is bad. – Torake Apr 20 '18 at 07:38

1 Answers1

0

Your usage of Response.Redirect is totally wrong, since .cshtml files are not accessed directly like webforms pages (.aspx) have (they need to be returned as view by controller action method). If you're working in MVC controller, use RedirectToAction instead.

if (Session["IdUsuario"] == null)
{
    return RedirectToAction("Index", "Home"); // replacement of Response.Redirect to root page
}
else
{
    if ((bool)Session["Temporal"] == true)
    {
        // use RedirectToAction instead of Response.Redirect
        return RedirectToAction("ContrasenaTemporal", "Login"); 
    }
    else
    { 
        // return something else
    }
}

Also the target action method should be like this:

// inside LoginController class
public ActionResult ContrasenaTemporal()
{
    // other stuff

    return View("ContraseñaTemporal");
}

Edit 1:

In case you want to redirect from a view page, you still need to follow virtual path in MVC with usage of Response.Redirect, pointing to given controller action method above:

@if (Session["IdUsuario"] == null)
{
    Response.Redirect("~/Home/Index");
}
else
{
    if ((bool)Session["Temporal"] == true)
    {
        Response.Redirect("~/Login/ContrasenaTemporal"); // follow MVC route convention
    }
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • the redirect to action is supposed to be used on the actionresults am i wrong? the thing is that im having this trouble on a view and it says redirect to action does not exist in the current context – Torake Apr 20 '18 at 07:34
  • `RedirectToAction` only used in controller action method, i.e. methods returning `ActionResult`, you can't use that in a view page. Which view page you're having trouble? – Tetsuya Yamamoto Apr 20 '18 at 07:40
  • I have this scenario, maybe i didnt explain myself well, i have a login, after the login it validates if the user have a temporal password if it has one, it should redirect to a change password view. after user logs in my actionresult method "Login" returns my view main that displays the menu ,and there is where my validation is (the one from the question) on the else that is empty is the html structure, those validations are with razor syntax – Torake Apr 20 '18 at 07:53
  • Well, apologize for misunderstanding. `Response.Redirect` still can be used to redirect from view page - but the URL in argument should follow MVC virtual path convention (e.g. `~/controllername/actionname`). However, I think better to check session state on controller side and use `RedirectToAction` to render targeted view if a user has temporal password. – Tetsuya Yamamoto Apr 20 '18 at 07:57