gpt4 book ai didi

asp.net-mvc - 重定向到 AuthorizeAttribute 中的某个 View ?

转载 作者:行者123 更新时间:2023-12-04 02:50:07 26 4
gpt4 key购买 nike

在我的 AdministratorController 中,我有一个带有自定义属性的操作:

[AuthorizedOnly (Roles = "admin, superadmin")]
public ActionResult Index()
{...}

属性是:

class AuthorizedOnlyAttribute : AuthorizeAttribute
{
public AuthorizedOnlyAttribute()
{
View = "~/Views/Main/Index.cshtml";
Master = String.Empty;
}
public String View { get; set; }
public String Master { get; set; }

public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
CheckIfUserIsAuthenticated(filterContext);
}
private void CheckIfUserIsAuthenticated(AuthorizationContext filterContext)
{
if(filterContext.Result == null)
return;
if(filterContext.HttpContext.User.Identity.IsAuthenticated)
{
if(String.IsNullOrEmpty(View))
return;
var result = new ViewResult{ViewName = View, MasterName = Master};

filterContext.Result = result;
}
}

它正确地显示了我需要的 View :~/Views/Main/Index.cshtml但在我的浏览器中,URL 仍然来自 Administrator Controller :.../Administrator/Index我如何才能重定向到我需要的 View ,以便该 URL 也会更改?

提前致谢!

最佳答案

试试这个

string retUrl = filterContext.HttpContext.Request.RawUrl;
filterContext.Result =
new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary
{{ "controller", "Main" },
{ "action", "Home" },
{ "returnUrl", retUrl } });

关于asp.net-mvc - 重定向到 AuthorizeAttribute 中的某个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com