0

trying to add/change the values of the parameters in the url, but can't able to change/add the values

trying to add/change the values of the parameters as shown below var Filename ='abc.pdf'; var strUser='john'; var url = '@Url.Action("Action","Controller", new{ filename="name", username="User" })'; window.location.href = url.replace('name',Filename + 'User', strUser);

but not able to do it

1
  • you can use params.set() method to update the existing parameter. Commented Jan 13, 2023 at 4:32

2 Answers 2

2

try this

var url = new URL('http://demourl.com/path?id=100&topic=main');
var search_params = url.searchParams;

// new value of "id" is set to "101"
search_params.set('id', '101');

// change the search property of the main url
url.search = search_params.toString();

// the new url string
var new_url = url.toString();

// output : http://demourl.com/path?id=101&topic=main
console.log(new_url);
Sign up to request clarification or add additional context in comments.

Comments

0

Try This:

var downloadUrl = '@Url.Action("Download", "File", new { filename = "default.pdf", username = "default" })';
var Filename = 'abc.pdf';
var strUser = 'john';
var url = downloadUrl.replace('default.pdf', Filename).replace('default', strUser);
window.location.href = url;

Comments

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.