C# 获取不带查询参数的当前页面URL-Razor Html帮助程序?
Razor中是否有一个方法返回当前页面URL,而不返回查询参数 我需要把它放入一个HTML助手方法中,我已经创建了一个字符串C# 获取不带查询参数的当前页面URL-Razor Html帮助程序?,c#,razor,visual-studio-2013,C#,Razor,Visual Studio 2013,Razor中是否有一个方法返回当前页面URL,而不返回查询参数 我需要把它放入一个HTML助手方法中,我已经创建了一个字符串 @Url似乎不起作用,如果我这样做了.ToString()我只会得到名称空间loll 剃须刀使用: <th width="100%" @Html.SortTableClickEvent(@Url.ToString(), "Name")> public static MvcHtmlString SortTableClickEvent(this Html
@Url
似乎不起作用,如果我这样做了.ToString()
我只会得到名称空间loll
剃须刀使用:
<th width="100%" @Html.SortTableClickEvent(@Url.ToString(), "Name")>
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);
}
<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>
将输出到我的html的内容:
<th width="100%" @Html.SortTableClickEvent(@Url.ToString(), "Name")>
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);
}
<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)
带着牢骚
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方法应该适合您。@Request.Path似乎也适合您@JamesT,这取决于你的需要。如果需要包含scheme和domain部分的完整路径,则将使用
GetLeftPart
。如果/controller/action
部分对您来说足够了,那么Request.Path
就可以了。JS:function GetParameterlessPath(action,relativeUrl){If(relativeUrl.indexOf(action)=-1)返回relativeUrl;返回relativeUrl.substring(0,relativeUrl.lastIndexOf(动作)+动作;}