获取当前页面URL而不使用查询参数 – Razor Html帮助程序?

Razor中是否有一个方法返回当前页面URL而不使用查询参数。

我需要把它推到我作为字符串创建的HTML帮助器方法中。

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

剃刀使用:

  

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:

  Name  

您可以使用Request.Url.GetLeftPart方法。 如果你说的是URL

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

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

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

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

  

没有查询字符串:

 Request.Url.GetLeftPart(UriPartial.Path) 

使用查询字符串

 Request.Url.PathAndQuery 

这就是我所使用的,它也可以解决任何动作的权利。

  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。 如果你有动作和relativeUrl(或任何url)底层方法应该适合你。