14

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

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

@Url does not seem to work and if I do .ToString() I just get the namespace LOLLL

Razor use:

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

Html helper:

    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);
    }

What gets output to my 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>

3 Answers 3

30

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

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

executing Request.Url.GetLeftPart(UriPartial.Path) should give

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

In code that might look like this:

<th width="100%" @Html.SortTableClickEvent(@Request.Url.GetLeftPart(UriPartial.Path), "Name")>
Sign up to request clarification or add additional context in comments.

2 Comments

@Request.Path seems to do the job too!
@JamesT, well, it depends on what your needs. If you need full path, with scheme and domain parts, then GetLeftPart is to be used. If /controller/action part is enough for you - then Request.Path will do indeed.
9

Without querystring:

Request.Url.GetLeftPart(UriPartial.Path)

With querystring

Request.Url.PathAndQuery

Comments

2

This is what I used which also works to get rid of ANYTHING the right of the action.

 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;
    }

This is obviously a static method, but I put it in my ActionFilter hence where I am getting the ActionExecutingContext. If you have the action and the relativeUrl (or any url for that matter) that bottom method should work for you.

1 Comment

And a version for JS: function GetParameterlessPath(action, relativeUrl) { if (relativeUrl.indexOf(action) == -1) return relativeUrl; return relativeUrl.substring(0, relativeUrl.lastIndexOf(action)) + action; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.