繁体   English   中英

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

[英]How to use Url.Action() in a class file?

如何在 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过滤器来实现这种行为更符合框架

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();

@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 ”。 希望这个答案能帮助那些遇到这个链接的读者。

我尝试使用@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");

为我工作。

对于那些迟到这篇文章的人,使用 .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);
        }

你也可以用这个

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

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

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

并在您的控制器上:

MyClass.CreateRoutingUrl(Url);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM