我使用共享布局,并且试图将当前位置传递给注销方法,但是我不确定如何执行此操作。我试过了:Request.Url.OriginalString,Request.Url.Scheme,在注销方法中,参数类型我将参数类型设置为字符串,但是它不起作用。有人知道吗?
布局页面上的注销按钮
<a onclick="location.href = '@Url.Action("Logoff", "Account", "Request.Url.Scheme")'"><span class="logOffSpan">Sign Out</span
public ActionResult LogOff(string url)
{
var local = Convert.ToBoolean(ConfigurationManager.AppSettings["local"]);
if (local)
{
FormsAuthentication.SignOut();
}
else
{
CasAuthentication.SingleSignOut();
}
Session.Clear();
return to Redirect("Some Method","Some Controller");//I want the user to just return to whatever page they were just on when they clicked log off
}
答案 0 :(得分:1)
您可以使用Request.Url.AbsoluteUri
来获取当前页面的绝对URL。
然后,您可以使用此@Url.Action
方法的重载,该方法将RouteValueDictionary
对象作为第三个参数来构建路由参数/查询字符串。
public virtual string Action (string actionName,
string controllerName,
RouteValueDictionary routeValues);
所以您的观看代码将是
<a href="@Url.Action("Logoff", "Account", new { url = Request.Url.AbsoluteUri })">
<span class="logOffSpan">Sign Out</span>
</a>
这基本上会生成将href
属性值设置为/Account/Logogg?url={yourCurrentPageUrl}
的anhcor标记,其中{yourCurrentPageUrl}
将被替换为您当前的页面URL。
现在,在您的操作方法中,您可以将Redirect
方法和该{URL}值一起使用,这将在url
参数中提供。
public ActionResult LogOff(string url)
{
// Your existing logic
return Redirect(url);
}