Asp.net mvc MVC重定向到操作不起作用-获取“;“找不到资源”;
在我的UserController中,我发布:Asp.net mvc MVC重定向到操作不起作用-获取“;“找不到资源”;,asp.net-mvc,Asp.net Mvc,在我的UserController中,我发布: return RedirectToAction("SignOut", "SignIn"); 但是得到: “/”应用程序中出现服务器错误 找不到资源。 描述:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除 已删除、名称已更改或暂时不可用。请查看以下URL和 确保拼写正确 请求的URL:/登录/注销 SignInController操作方法: [HttpPost] public ActionRe
return RedirectToAction("SignOut", "SignIn");
但是得到:
“/”应用程序中出现服务器错误
找不到资源。
描述:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除
已删除、名称已更改或暂时不可用。请查看以下URL和
确保拼写正确
请求的URL:/登录/注销
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");
}
我有一个局部视图,它引用了相同的操作方法,但有POST方法:
@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>
}
@if((字符串)@Session[“IsAuthenticated”]==“true”)
{
使用(Html.BeginForm(“SignOut”、“SignIn”、FormMethod.Post、new{id=
“signoffForm”,@class=“navbar right”})
{
@Html.AntiForgeryToken()
-
}
}
其他的
{
- @ActionLink(“注册”、“注册”、“注册”,
RouteValue:null,htmlAttributes:new{id=“registerLink”})
- @ActionLink(“登录”、“登录”、“登录”、路由值:
null,htmlAttributes:new{id=“signLink”})
}
重定向到操作
,正在执行一个HTTP GET
您的注销
方法被[HttpPost]
修饰,因此,它找不到资源
由于
注销
是一种典型的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");
}
Stefan…我的局部视图说明了Post方法,因此我用它来装饰我的方法。那么我如何告诉RedirectToAction它是一个Post方法呢?我尝试将[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]放在[HttpPost]下面,但它没有做任何操作。我删除了[HttpPost]并将其替换为[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]。现在它找到了。Stefan…我实际上有两个控制器,它们重定向到SignInController操作方法。因此,如果我要使用您的建议,我必须在全局方法中复制该操作方法代码。。所以我会在两个地方有相同的代码。这是明智之举还是像我那样在1处使用装饰?我删除了[HttpPost]并将其替换为[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]。现在它找到了。我可以组合请求方法属性,因为此操作服务于来自多个谓词的请求。
[HttpPost]
public ActionResult OtherAction()
{
Helper.SignOut(Response, Session);
return RedirectToAction("Index", "Home");
}