Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在HTML帮助程序中生成URL_Asp.net Mvc_Url_Html Helper - Fatal编程技术网

Asp.net mvc 在HTML帮助程序中生成URL

Asp.net mvc 在HTML帮助程序中生成URL,asp.net-mvc,url,html-helper,Asp.net Mvc,Url,Html Helper,通常,在ASP.NET视图中,可以使用以下函数获取URL(而不是): 但是,我无法从自定义HTML帮助器中找到如何执行此操作。我有 public class MyCustomHelper { public static string ExtensionMethod(this HtmlHelper helper) { } } helper变量具有Action和GenerateLink方法,但它们生成。我在ASP.NETMVC源代码中做了一些挖掘,但是我找不到一个简单的方法 问题

通常,在ASP.NET视图中,可以使用以下函数获取URL(而不是
):

但是,我无法从自定义HTML帮助器中找到如何执行此操作。我有

public class MyCustomHelper
{
   public static string ExtensionMethod(this HtmlHelper helper)
   {
   }
}
helper变量具有Action和GenerateLink方法,但它们生成
。我在ASP.NETMVC源代码中做了一些挖掘,但是我找不到一个简单的方法

问题是上面的Url是view类的一个成员,对于它的实例化,它需要一些上下文和路由映射(我不想处理这些,我也不想处理这些)。或者,HtmlHelper类的实例也有一些上下文,我假设它们是Url实例的上下文信息子集的一部分(但我不想再处理它)

总而言之,我认为这是可能的,但由于我所看到的所有方法都涉及到一些或多或少的内部ASP.NET内容的操作,我想知道是否有更好的方法

编辑:例如,我看到的一种可能性是:

public class MyCustomHelper
{
    public static string ExtensionMethod(this HtmlHelper helper)
    {
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        urlHelper.Action("Action", "Controller");
    }
}

但这似乎并不正确。我不想处理我自己的例子。一定有更简单的方法。

您可以在html帮助程序扩展方法中创建url帮助程序,如下所示:

var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Action("Home", "Index")

您还可以使用
UrlHelper
public和static类获取链接:

UrlHelper.GenerateUrl(null, actionName, controllerName, null, null, null, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true)

在本例中,您不必创建新的UrlHelper类,这可能有一点好处。

下面是我的小扩展方法,用于获取
HtmlHelper
实例的
UrlHelper

  public static partial class UrlHelperExtensions
    {
        /// <summary>
        /// Gets UrlHelper for the HtmlHelper.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns></returns>
        public static UrlHelper UrlHelper(this HtmlHelper htmlHelper)
        {
            if (htmlHelper.ViewContext.Controller is Controller)
                return ((Controller)htmlHelper.ViewContext.Controller).Url;

            const string itemKey = "HtmlHelper_UrlHelper";

            if (htmlHelper.ViewContext.HttpContext.Items[itemKey] == null)
                htmlHelper.ViewContext.HttpContext.Items[itemKey] = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

            return (UrlHelper)htmlHelper.ViewContext.HttpContext.Items[itemKey];
        }
    }

(我发布此ans仅供参考)

我意识到这是一个简化的示例,但对于所示的示例,我将扩展UrlHelper而不是HtmlHelper。不过,您真正的代码可能需要这两个。对不起,我应该更清楚:我想在扩展方法中进行一些HTML渲染,我需要为它生成URL。我更喜欢这个答案,因为它设置了RouteCollection。我认为如果构造函数也初始化RouteCollection
new UrlHelper会更好(htmlHelper.ViewContext.RequestContext,htmlHelper.RouteCollection)
这是一种非常好的方法,因为它重用了一个现有的对象,而不是创建一个新的对象。我们正在使用ASP.NET 4.5,并且遇到了重入问题。我们不相信UrlHelper可以跨http请求重用。请注意。
  public static partial class UrlHelperExtensions
    {
        /// <summary>
        /// Gets UrlHelper for the HtmlHelper.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns></returns>
        public static UrlHelper UrlHelper(this HtmlHelper htmlHelper)
        {
            if (htmlHelper.ViewContext.Controller is Controller)
                return ((Controller)htmlHelper.ViewContext.Controller).Url;

            const string itemKey = "HtmlHelper_UrlHelper";

            if (htmlHelper.ViewContext.HttpContext.Items[itemKey] == null)
                htmlHelper.ViewContext.HttpContext.Items[itemKey] = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

            return (UrlHelper)htmlHelper.ViewContext.HttpContext.Items[itemKey];
        }
    }
public static MvcHtmlString RenderManagePrintLink(this HtmlHelper helper, )
{    
    var url = htmlHelper.UrlHelper().RouteUrl('routeName');
    //...
}