【问题标题】:Appending to current URL in ASP.NET MVC在 ASP.NET MVC 中附加到当前 URL
【发布时间】:2010-11-15 00:39:27
【问题描述】:

我知道这可能很容易实现,但我很难找到答案。类似于 Stackoverflow 允许您继续使用标签深入研究问题,我正在尝试实现相同的功能。因此,我在 MVC 页面上创建了一个“标签”列表,如下所示:

<%For Each item As myItem In ViewData("topTags")%>
    <li><%=Html.ActionLink(Html.Encode(item.tagName), "Index", New With {.tags = item.tagName})%</li>
<%Next%>

这会生成类似于http://mysite.com/controller/index?tags=MyTageName 的漂亮网址

我有一个控制器方法,它采用 tagName 参数值,我们很高兴……在第一级。我希望能够获取此处返回的视图,并且对于上面代码中的每个标签,将新标签名称附加到 url。我新生成的 URL 如下所示:

http://mysite.com/controller/index?tags=TagNameIClickedBefore+TagNameInForEachLoop

希望这是有道理的...有什么想法吗?编写我自己的 HTMLHelper?我缺少 URL.namespace 中的魔术功能? :)

【问题讨论】:

    标签: asp.net-mvc friendly-url


    【解决方案1】:

    我想这就是你想要做的:

    <%For Each item As myItem In ViewData("topTags")%>
       <li><%=Html.ActionLink(Html.Encode(item.tagName), "Index", 
          New With {.tags = Request.QueryString("tags") & "+" & item.tagName})%></li>
    <%Next%>
    

    【讨论】:

    • 做到了,而且非常简单。我有一个 if 然后检查 request.querystring("Tags") 在循环中是否为空。还有一个问题,如何在不进行 htmlEncoded 的情况下显示 + 号,或者这只是一种不好的做法?
    • 我认为如果你只使用空格而不是 + 号会更好。
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多