【发布时间】:2017-08-28 13:20:54
【问题描述】:
所以我试图向用户显示一条消息,当他从无权查看的页面重定向时。示例:用户访问 www.mypage.com/users 并被重定向到 www.mypage.com/home。
我使用 MVC 模式,它是 asp.net Web 应用程序。 我重写 AuthorizeAttribute 并在 AuthorizeCore 方法中尝试这样做:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (false)//Here is custom logic that is working
{
string message = "You don't have an access to selected menu item.";
var dataDict = HttpContext.Current.Session["__ControllerTempData"] as IDictionary<string, object>;
if (dataDict == null)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary["myErrorMessage"] = message;
HttpContext.Current.Session["__ControllerTempData"] = dictionary;
}
else
{
dataDict["myErrorMessage"] = message;
HttpContext.Current.Session["__ControllerTempData"] = dataDict;
}
_isAuthorized = false;
httpContext.Response.Redirect("Home");
}
else
{
_isAuthorized = base.AuthorizeCore(httpContext);
}
return _isAuthorized;
}
然后我尝试从视图中访问它
var unauthorized_access_message = TempData["myErrorMessage"] as List <string> ?? new List<string>() ;
但它不起作用。我也尝试了this,但情况并非如此,因为我尝试访问一个控制器然后重定向到另一个控制器。 是否有任何解决方案可以将变量传递给视图或检查视图中的某些状态(如重定向原因)?
【问题讨论】:
标签: asp.net httpcontext