问题描述
在我的UserController中,我发出:
return RedirectToAction("SignOut","SignIn");
但得到:
“ /”应用程序中的服务器错误。
找不到资源。 说明:HTTP404。您正在寻找的资源(或其依赖项之一)可能已经 删除,更名或暂时不可用。请查看以下网址和 确保拼写正确。
请求的URL:/ SignIn / SignOut
SignInController操作方法:
[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");
}
@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",routeValues: null,htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Sign in",routeValues:
null,htmlAttributes: new { id = "signLink" })</li>
</ul>
}
解决方法
RedirectToAction
,正在执行HTTP GET
您的SignOut
方法用[HttpPost]
装饰,因此找不到资源。
由于SignOut
是典型的post
动作-即;按下按钮之类的方法,我建议将签出逻辑放在单独的方法中,然后从要使用它的地方调用它。这意味着您不需要重定向即可执行注销。
根据您的评论;您可以创建一个辅助方法来执行任务:
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();
}
}
,然后在两个函数中调用它;您可以重定向-我现在没有可使用的编译器,但是我认为您可以解决所有不正确的地方。
[HttpPost]
public ActionResult SignOut()
{
Helper.SignOut(Response,Session);
return RedirectToAction("Index","Home");
}
[HttpPost]
public ActionResult OtherAction()
{
Helper.SignOut(Response,"Home");
}