【问题标题】:Get current page URL without query parameters - Razor Html helper?获取没有查询参数的当前页面 URL - Razor Html 助手?
【发布时间】:2015-02-20 04:09:30
【问题描述】:

Razor 中是否有一个方法可以返回当前页面的 URL 而不带查询参数。

我需要把它塞进一个我创建为字符串的 HTML 辅助方法中。

@Url 似乎不起作用,如果我这样做 .ToString() 我只会得到命名空间 LOLLL

剃须刀使用:

<th width="100%" @Html.SortTableClickEvent(@Url.ToString(), "Name")>

HTML 助手:

    public static MvcHtmlString SortTableClickEvent(this HtmlHelper html, string url, string column)
    {
        StringBuilder sortingPropertiesObject = new StringBuilder();
        sortingPropertiesObject.Append("var properties = new James.prototype.Table.SortingProperties();");
        sortingPropertiesObject.Append("properties.url = \"" + url + "\"");
        sortingPropertiesObject.Append("properties.colName = \"" + column + "\"");

        string clickEvent = "onclick = James.Table.SortByColumn(properties, this);";

        return MvcHtmlString.Create(sortingPropertiesObject + clickEvent);
    }

输出到我的 html 的内容:

<th width="100%" onclick='James.Table.SortByColumn("Name",' this);="" properties.colname="Name" james.prototype.table.sortingproperties();properties.url="System.Web.Mvc.UrlHelper" properties="new" var="">
            Name
        </th>

【问题讨论】:

    标签: c# razor visual-studio-2013


    【解决方案1】:

    您可以为此使用Request.Url.GetLeftPart 方法。如果你的网址是说

    http://the-site.com/controller/action?param=1
    

    执行Request.Url.GetLeftPart(UriPartial.Path)应该给

    http://the-site.com/controller/action
    

    在可能如下所示的代码中:

    <th width="100%" @Html.SortTableClickEvent(@Request.Url.GetLeftPart(UriPartial.Path), "Name")>
    

    【讨论】:

    • @Request.Path 似乎也可以完成这项工作!
    • @JamesT,好吧,这取决于您的需求。如果您需要完整路径,包括方案和域部分,那么将使用 GetLeftPart。如果/controller/action 对你来说足够了 - 那么Request.Path 确实可以。
    【解决方案2】:

    没有查询字符串:

    Request.Url.GetLeftPart(UriPartial.Path)
    

    使用查询字符串

    Request.Url.PathAndQuery
    

    【讨论】:

      【解决方案3】:

      这是我使用的,它也可以用来摆脱任何行为的权利。

       public static string GetParameterlessPath(ActionExecutingContext filterContext)
          {
              return GetParameterlessPath(filterContext.ActionDescriptor.ActionName,
                  VirtualPathUtility.ToAppRelative(filterContext.HttpContext.Request.Path));
          }
      
          public static string GetParameterlessPath(string action, string relativeUrl)
          {
              return  relativeUrl.Contains(action) 
                   ? relativeUrl.Substring(0, relativeUrl.LastIndexOf(action))+action 
                   : relativeUrl;
          }
      

      这显然是一个静态方法,但我把它放在我的 ActionFilter 中,因此我得到了 ActionExecutingContext。如果你有 action 和 relativeUrl(或任何 url),那么底部方法应该适合你。

      【讨论】:

      • 还有一个 JS 版本:function GetParameterlessPath(action, relativeUrl) { if (relativeUrl.indexOf(action) == -1) return relativeUrl;返回 relativeUrl.substring(0, relativeUrl.lastIndexOf(action)) + 动作; }
      猜你喜欢
      • 2011-10-24
      • 2016-05-22
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2017-09-12
      • 2015-04-01
      相关资源
      最近更新 更多