28

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

7 回答 7

47

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

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

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

于 2014-01-01T13:54:15.097 回答
6

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();
于 2014-01-01T13:55:06.977 回答
1

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

于 2019-05-15T12:39:51.613 回答
0

你也可以使用这个

return RedirectToAction("Index", "MainPage");
于 2020-12-14T03:59:47.783 回答
0

对于那些迟到这篇文章的人,使用 .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);
        }
于 2020-12-09T07:56:30.657 回答
0

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

为我解决了。

于 2018-09-11T20:33:58.010 回答
0

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

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

在你的控制器上:

MyClass.CreateRoutingUrl(Url);
于 2021-03-20T10:36:56.057 回答