Get Url Without Querystring

Is there any method to get the URL without query string?

Try this:

let path = window.location.href.split('?')[0]
console.log({path})

Get url without querystring

You can use System.Uri

Uri url = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = String.Format("{0}{1}{2}{3}", url.Scheme,
Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);

Or you can use substring

string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.Substring(0, url.IndexOf("?"));

EDIT: Modifying the first solution to reflect brillyfresh's suggestion in the comments.

How to get the URL without the Query String or the Id part?

Looks like the only two things you need from URL are action and controller and if the action is "index" you don't need it. In this case I believe that you are doing it right. This is a slightly modified piece of code I was using in ASP.NET MVC project:

string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
if (actionName.Equals("index", StringComparison.InvariantCultureIgnoreCase))
{
actionName = string.Empty;
}
string result = $"/{controllerName}/{actionName}";

There is one more thing to mention: "areas". Once I was working on website which had them, so they may appear in your URL too if your website uses "area" approach.

I hope it helps /p>

Javascript get url without querystring

To get all the different parts of a url, location.protocol + '//' + location.host + location.pathname would be the correct syntax. Here's an example displaying the url where this snippet is hosted:

document.body.innerHTML = "The snippet is at this web address: " + getURL();
function getURL() { return location.protocol + '//' + location.host + location.pathname}

Get current page URL without query parameters - Razor Html helper?

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")>

Match Url path without query string

No need for regexp:

url.split("?")[0];

If you really need it, then try this:

\/path\?*.*

EDIT Actually the most precise regexp should be:

^(\/path)(\/?\?{0}|\/?\?{1}.*)$

because you want to match either /path or /path/ or /path?something or /path/?something and nothing else. Note that ? means "at most one" while \? means a question mark.

BTW: What kind of routing library does not handle query strings?? I suggest using something else.

How to get the URL without any parameters in JavaScript?

This is possible, but you'll have to build it manually from the location object:

location.protocol + '//' + location.host + location.pathname


Related Topics



Leave a reply



Submit