Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Quick question! How do I replace a query string parameter in an url? Lets say I want to replace the width parameter in the string below, with width=360?
/media/1010/demo.png?width=300&height=200
In javascript I would do something like:
url.replace(/width=\d+/, "width=360");
But how to do this in Razor (partial view macro)?
Happy friday!
Hey Mathias,
Something like this should work:
Regex.Replace("your url", @"([?&]width)=[^?&]+", "$1=200")
Cheers, Tom
Also just as a side note, a great site I find really useful for helping to write regular expressions is: http://regexr.com/ :D
Great! It works - thanks
Can you tell me how to remove a parameter. Lets say I wanted to remove the height parameter from the string?
Great sidenote btw!
You could use the same regex but change it to replace with an empty string e.g.
Regex.Replace("your url", @"([?&]height)=[^?&]+", "")
Awesome! Thanks!
you can try this
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor @{ var url = HttpContextAccessor.HttpContext.Request; string currentURL = string.Concat(url.Host, url.Path,"?item=");
}
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Replace query string parameter with a regular expression
Quick question! How do I replace a query string parameter in an url? Lets say I want to replace the width parameter in the string below, with width=360?
/media/1010/demo.png?width=300&height=200
In javascript I would do something like:
But how to do this in Razor (partial view macro)?
Happy friday!
Hey Mathias,
Something like this should work:
Cheers, Tom
Also just as a side note, a great site I find really useful for helping to write regular expressions is: http://regexr.com/ :D
Great! It works - thanks
Can you tell me how to remove a parameter. Lets say I wanted to remove the height parameter from the string?
Great sidenote btw!
You could use the same regex but change it to replace with an empty string e.g.
Cheers, Tom
Awesome! Thanks!
you can try this
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor @{ var url = HttpContextAccessor.HttpContext.Request; string currentURL = string.Concat(url.Host, url.Path,"?item=");
}
is working on a reply...