c# - 如何在类文件中使用 Url.Action()?

标签 c# asp.net-mvc url.action

如何在 MVC 项目的类文件中使用 Url.Action()?

喜欢:

namespace _3harf
{
    public class myFunction
    {
        public static void CheckUserAdminPanelPermissionToAccess()
        {
            if (ReferenceEquals(HttpContext.Current.Session["Loged"], "true") &&
                myFunction.GetPermission.AdminPermissionToLoginAdminPanel(
                    Convert.ToInt32(HttpContext.Current.Session["UID"])))
            {
                HttpContext.Current.Response.Redirect(Url.Action("MainPage", "Index"));
            }
        }
    }
}

最佳答案

您需要手动创建 UrlHelper类并传递适当的 RequestContext。它可以通过类似的方式完成:

var requestContext = HttpContext.Current.Request.RequestContext;
new UrlHelper(requestContext).Action("Index", "MainPage");

但是,您正试图实现基于身份验证的重定向。我建议你看看实现自定义 AuthorizeAttribute filter实现这种行为更符合框架

关于c# - 如何在类文件中使用 Url.Action()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20869222/

相关文章:

c# - 如何解决时区转换错误?

javascript - Url.Action 在 VS IDE 中的 JavaScript 中将先前的 URL 与当前的路由参数结合起来

c# - ASP.NET MVC 将 ActionLink 中的字符串传递给 Controller

c# - 跨表SQL查询

c# - 根据日期时间比较两个文件最多一分钟?

asp.net-mvc - 多个 DbContexts 不同的数据库

c# - 字典中的 “An item with the same key has already been added”

c# - MVC 错误 : The model item passed into the dictionary is null

c# - Url.Action() 包含不需要的参数,试图获得更多控制

asp.net-mvc-3 - 我如何将整数列表添加到路由