【问题标题】:How to use Url.Action() in a class file?如何在类文件中使用 Url.Action()?
【发布时间】:2014-01-19 02:10:40
【问题描述】:

如何在 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"));
            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢伙计,你又救了我的命 :) 或时间(?) :} 爱你
    • 应该是 Action("", "") 而不是上面的 Action("", "")
    • @johnstaveley 没错。除了语义,答案不要尝试解密 Ercin 是否已反转值并使用与原始问题中提供的值相同的值。
    【解决方案2】:

    RequestContext 从控制器传递给您的自定义类。我会在您的自定义类中添加一个构造函数来处理这个问题。

    using System.Web.Mvc;
    public class MyCustomClass
    {
        private UrlHelper _urlHelper;
        public MyCustomClass(UrlHelper urlHelper)
        {
            _urlHelper = urlHelper;
        }
        public string GetThatURL()
        {         
          string url=_urlHelper.Action("Index", "Invoices"); 
          //do something with url or return it
          return url;
        }
    }
    

    您需要将System.Web.Mvc 命名空间导入该类才能使用UrlHelper 类。

    现在在您的控制器中,创建MyCustomClass 的对象并在构造函数中传递控制器上下文,

    UrlHelper uHelp = new UrlHelper(this.ControllerContext.RequestContext);
    var myCustom= new MyCustomClass(uHelp );    
    //Now call the method to get the Paging markup.
    string thatUrl= myCustom.GetThatURL();
    

    【讨论】:

    • 非常感谢完全面向对象的工作,但西蒙的解决方案易于使用。我标记为书签非常感谢!喜欢...
    【解决方案3】:

    @Simon Belanger 的答案非常有效,但UrlHelper.Action() 生成相对 URL,在我的情况下,我需要完全限定的绝对 URL。所以我需要做的是 - 我必须使用UrlHelper.Action() 方法提供的重载之一。

    var requestContext = HttpContext.Current.Request.RequestContext;
    string link = new UrlHelper(requestContext).Action("Index", "Home", null, HttpContext.Current.Request.Url.Scheme);
    

    假设您的应用程序托管在“https://myexamplesite.com”上,那么上面的代码将为您提供像这样的完整网址 - “https://myexamplesite.com/Home/Index”。希望这个答案能帮助那些会遇到这个链接的读者。

    【讨论】:

      【解决方案4】:

      我尝试使用@simion 的答案,但我在 UrlHelper 的构造函数中得到了无效类型。 “无法从 System.Web.Routing.RequestContext 转换为 System.Net.Http.HttpRequestMessage”

      所以我用了这个

      var urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext); string url = urlHelper.Action("MainPage", "Index");

      为我解决了。

      【讨论】:

        【解决方案5】:

        你也可以用这个

        return RedirectToAction("Index", "MainPage");
        

        【讨论】:

          【解决方案6】:

          对于那些迟到这篇文章的人,使用 .Net Core 和 .Net 5.0,你应该试试这个;

          private readonly IUrlHelperFactory _urlHelperFactory;
          private readonly IActionContextAccessor _actionContextAccessor;
          
           public EmailSenderService(IUrlHelperFactory urlHelperFactory,
                      IActionContextAccessor actionContextAccessor)
                  {
                      _urlHelperFactory = urlHelperFactory;
                      _actionContextAccessor = actionContextAccessor;
                  }
          
           private string GenerateUrl(string action, string controller, object routeValues = null)
                  {
                      var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
                      return urlHelper.Action(action, controller, routeValues, _actionContextAccessor.ActionContext.HttpContext.Request.Scheme);
                  }
          

          【讨论】:

          • 我在 6 多年前问过这个问题。对于遗留框架 ofc。但谢谢你的控制。
          • 我只是没有回答你。如果你注意我已经分享了如何为 .Net Core 和 .Net 5.0 做这件事。
          【解决方案7】:

          您只需将 Url 属性从控制器传递到您的类文件,

          string CreateRoutingUrl(IUrlHelper url)
          {
              return url.Action("Action", "Controller");
          }
          

          在你的控制器上:

          MyClass.CreateRoutingUrl(Url);
          

          【讨论】:

            猜你喜欢
            • 2015-07-13
            • 1970-01-01
            • 1970-01-01
            • 2011-05-15
            • 2015-02-11
            • 1970-01-01
            • 2020-05-14
            • 1970-01-01
            相关资源
            最近更新 更多