ASP.NET MVC - HTML 扩展方法构建 URL 或链接

发布于 2024-08-17 02:47:20 字数 989 浏览 4 评论 0原文

考虑一个扩展方法,其目的是:

  • 在某些条件下呈现 标记
  • ,仅返回不带链接的字符串

  问题:在扩展方法中,如何利用路由值等正确的路由逻辑,而不是对字符串进行硬编码。我怀疑 HtmlHelper.GenerateRouteLink 是解决方案的一部分,但请建议实现此目的的最佳方法。

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    //would like to do this correctly!
    return string.Format("<a href='/MyAppName/User/View/{0}' title='{2}'>{1}</a>"
                        , user.Mnemonic, user.DisplayName, user.Location);

    //normally returns http://mysite.net/MyAppName/User/View/FOO
    }

更多信息:

  • 使用 ASP.NET MVC 1.0

alt text

Consider an extension method whose purpose is to either:

  • render an <a> tag
  • on some condition, just return a string without a link

 
Question: in an extension method, how can you leverage the proper routing logic with Route Values, etc. rather than hardcoding the string. I suspect HtmlHelper.GenerateRouteLink is part of the solution, but please suggest the best way to achieve this.

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    //would like to do this correctly!
    return string.Format("<a href='/MyAppName/User/View/{0}' title='{2}'>{1}</a>"
                        , user.Mnemonic, user.DisplayName, user.Location);

    //normally returns http://mysite.net/MyAppName/User/View/FOO
    }

More info:

  • using ASP.NET MVC 1.0

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-08-24 02:47:20

我昨天只需要做类似的事情。可能有一种更巧妙的方法来做到这一点,但它可以帮助我准确地了解正在发生的事情,所以我不做任何假设。

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    RouteValueDictionary routeValues = new RouteValueDictionary();

    routeValues.Add("controller", "User");
    routeValues.Add("action", "View");
    routeValues.Add("id", user.Mnemonic);

    UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext);
    TagBuilder linkTag = new TagBuilder("a");

    linkTag.MergeAttribute("href", urlHelper.RouteUrl(routeValues));
    linkTag.MergeAttribute("title", user.Location);
    linkTag.InnerHtml = user.DisplayName;

    return linkTag.ToString(TagRenderMode.Normal);
}

I just had to do something similar to this yesterday. There may be a slicker way to do it, but it helps me to see exactly what is going on, so I don't assume anything.

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    RouteValueDictionary routeValues = new RouteValueDictionary();

    routeValues.Add("controller", "User");
    routeValues.Add("action", "View");
    routeValues.Add("id", user.Mnemonic);

    UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext);
    TagBuilder linkTag = new TagBuilder("a");

    linkTag.MergeAttribute("href", urlHelper.RouteUrl(routeValues));
    linkTag.MergeAttribute("title", user.Location);
    linkTag.InnerHtml = user.DisplayName;

    return linkTag.ToString(TagRenderMode.Normal);
}
假装爱人 2024-08-24 02:47:20

这行得通吗?

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    return html.ActionLink(user.DisplayName, "user", "View", new {title=user.Location});
    //normally returns http://mysite.net/MyAppName/User/View/FOO
}

would this work?

public static string CreateUserLink(this HtmlHelper html, string userAcctName)
{
    if (string.IsNullOrEmpty(userAcctName))
        return "--Blank--";

    //some lookup to A.D.            
    DomainUser user = ADLookup.GetUserByAcctName(userAcctName);

    if (user == null)
        return userAcctName;

    return html.ActionLink(user.DisplayName, "user", "View", new {title=user.Location});
    //normally returns http://mysite.net/MyAppName/User/View/FOO
}
メ斷腸人バ 2024-08-24 02:47:20

我对GenerateRouteLink 的体验是一场艰苦的战斗。自从我搞乱它以来已经有一段时间了,但如果这是我想到的方法,微软已将其设置为“内部”,因此您无法在 MVC 程序集之外访问和使用它。有很多我尝试过但并不真正喜欢的解决方法。

为了避免在辅助方法中对 url 进行硬编码,我最终所做的就是让它接受“字符串 url”参数,并在调用辅助方法时在我的视图中使用 Url.Action。这不是最干净的,但它是一种对我来说效果很好的解决方法。

<%= Html.CreateUserLink("userAcctName", Url.Action("Home", "Controller") %>

My experience with GenerateRouteLink has been an uphill battle. It's been a while since I messed with it but if it's the Method I'm thinking of Microsoft has made it "internal" so you can't access and use it outside the MVC assembly. There are a number of workarounds that I played with and didn't really like.

What I ended up doing to avoid hard coding the url in my helper methods is have it accept a 'string url' parameter and use Url.Action in my view when I call the helper method. It's not the cleanest but it's a workaround that worked well for me.

<%= Html.CreateUserLink("userAcctName", Url.Action("Home", "Controller") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文