'MVC RedirectToAction not working - getting "The resource cannot be found"

In my UserController, I am issuing:

return RedirectToAction("SignOut", "SignIn");

but getting:

Server Error in '/' Application.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /SignIn/SignOut

The SignInController action method:

[HttpPost]
public ActionResult SignOut()
{
    // Setting cache.
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
    Response.Cache.SetNoStore();

    // Setting cookies.
    // Instantiate a Cookie.
    HttpCookie Cookies = new HttpCookie("Gbng");
    Cookies.Value = "";
    Cookies.Expires = DateTime.Now.AddHours(-1);
    Response.Cookies.Add(Cookies);

    HttpContext.Session.Clear();

    Session.Abandon();

    // Redirect to the Home controller.
    return RedirectToAction("Index", "Home");
}

I have a partial view that references the same action method but has the POST method:

@if ( (string)@Session["IsAuthenticated"] == "true" )
{
    using (Html.BeginForm("SignOut", "SignIn", FormMethod.Post, new { id = 
"signoffForm", @class = "navbar-right" }))
    {
        @Html.AntiForgeryToken()

        <ul class="nav navbar-nav navbar-right">
           <li>
               <a 
href="javascript:document.getElementById('signoffForm').submit()">Sign 
out</a>
            </li>
        </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Register", 
routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Sign in", "SignIn", "SignIn", routeValues: 
null, htmlAttributes: new { id = "signLink" })</li>
    </ul>
}


Solution 1:[1]

RedirectToAction, is performing a HTTP GET

Your SignOut method is decorated with [HttpPost], and hence, it can not find the resource.


Since SignOut is a typical post action - i.e.; pressing a button and such, I would suggest to put the sign-out logic in a separate method and call it from where you want to use it. This means you don't need a redirect to perform the sign-off.


As from your comment; you can create a helper method to perform the task:

public class Helper
{
    public static void SignOut(System.Web.SessionState.HttpSessionState session,
                               HttpResponse response)
    {
        // Setting cache.
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        response.Cache.SetNoStore();

        // Setting cookies.
        // Instantiate a Cookie.
        HttpCookie Cookies = new HttpCookie("Gbng");
        Cookies.Value = "";
        Cookies.Expires = DateTime.Now.AddHours(-1);
        response.Cookies.Add(Cookies);

        session.Clear();

        session.Abandon();
    }
}

and call it in both functions, after that; you can redirect - I don't have a compiler at my disposal right now, but I think you can work out any inaccuracies.

[HttpPost]
public ActionResult SignOut()
{
    Helper.SignOut(Response, Session);
    return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult OtherAction()
{
    Helper.SignOut(Response, Session);
    return RedirectToAction("Index", "Home");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1