获取没有查询参数的当前页面 URL - Razor Html 助手? [英] Get current page URL without query parameters - Razor Html helper?

查看:16
本文介绍了获取没有查询参数的当前页面 URL - Razor Html 助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Razor 中是否有返回当前页面 URL 而不带查询参数的方法.

Is there a method in Razor that returns the current pages URL without the query parameters.

我需要将它放入我创建为字符串的 HTML 帮助器方法中.

I need to shove it into an HTML helper method I have created as a string.

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

@Url does not seem to work and if I do .ToString() I just get the namespace 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 方法来解决这个问题.如果您的网址是说

You can use Request.Url.GetLeftPart method for that. If your URL is say

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

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

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

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

In code that might look like this:

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

这篇关于获取没有查询参数的当前页面 URL - Razor Html 助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆