asp.net-mvc 在 HTML 助手中生成 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1443647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate URL in HTML helper
提问by Jan Zich
Normally in an ASP.NET view one could use the following function to obtain a URL (not an <a>):
通常在 ASP.NET 视图中,可以使用以下函数来获取 URL(不是<a>):
Url.Action("Action", "Controller");
However, I cannot find how to do it from a custom HTML helper. I have
但是,我无法从自定义 HTML 帮助程序中找到如何执行此操作。我有
public class MyCustomHelper
{
public static string ExtensionMethod(this HtmlHelper helper)
{
}
}
The helper variable has the Action and GenerateLink methods, but they generate <a>'s. I did some digging in the ASP.NET MVC source code, but I could not find a straightforward way.
helper 变量有 Action 和 GenerateLink 方法,但它们生成<a>的。我对 ASP.NET MVC 源代码进行了一些挖掘,但找不到直接的方法。
The problem is that the Url above is a member of the view class and for its instantiation it needs some contexts and route maps (which I don't want to be dealing with and I'm not supposed to anyway). Alternatively, the instance of the HtmlHelper class has also some context which I assume is either supper of subset of the context information of the Url instance (but again I don't want to dealing with it).
问题是上面的 Url 是视图类的成员,它的实例化需要一些上下文和路由映射(我不想处理,无论如何我也不应该处理)。或者,HtmlHelper 类的实例也有一些上下文,我假设它是 Url 实例的上下文信息子集的超值(但我又不想处理它)。
In summary, I think it is possible but since all ways I could see, involve some manipulation with some more or less internal ASP.NET stuff, I wonder whether there is a better way.
总之,我认为这是可能的,但由于我所看到的所有方式都涉及对一些或多或少的内部 ASP.NET 内容的一些操作,我想知道是否有更好的方法。
Edit:For instance, one possibility I see would be:
编辑:例如,我看到的一种可能性是:
public class MyCustomHelper
{
public static string ExtensionMethod(this HtmlHelper helper)
{
UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
urlHelper.Action("Action", "Controller");
}
}
But it does not seem right. I don't want to be dealing with instances of UrlHelper myself. There must be an easier way.
但似乎不对。我不想自己处理 UrlHelper 的实例。必须有更简单的方法。
回答by Darin Dimitrov
You can create url helper like this inside html helper extension method:
您可以在 html helper 扩展方法中创建这样的 url helper:
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Action("Home", "Index")
回答by cryss
You can also get links using UrlHelperpublic and static class:
您还可以使用UrlHelper公共和静态类获取链接:
UrlHelper.GenerateUrl(null, actionName, controllerName, null, null, null, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true)
In this example you don't have to create new UrlHelper class what could be a little advantage.
在这个例子中,你不必创建新的 UrlHelper 类,这可能是一个小优势。
回答by Kibria
Here is my tiny extenstion method for getting UrlHelperof a HtmlHelperinstance :
这里是让我的小extenstion方法UrlHelper一的HtmlHelper实例:
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];
}
}
Use it as:
将其用作:
public static MvcHtmlString RenderManagePrintLink(this HtmlHelper helper, )
{
var url = htmlHelper.UrlHelper().RouteUrl('routeName');
//...
}
(I'm posting this ans for reference only)
(我发布此答案仅供参考)

