[英]Get current page URL without query parameters - Razor Html helper?
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>
您可以為此使用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.Url.GetLeftPart(UriPartial.Path)
用querystring
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),那么bottom方法應該適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.