0

Using a Javascript function, I pass in the URL I want to redirect to in Chrome browser to the function called redirect2MainLookup. Each time it prepends the current page directory to the redirect location, and gives the following error:

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /LookupCodes/Edit/~/LookupCodes/Character.cshtml

Below is the function where I have tried many of the redirect options found at How do I redirect to another webpage?

HTML: '''

onclick="redirect2MainLookup('~/LookupCodes/Character.cshtml')">@Page.Title'''

Javascript function:

function redirect2MainLookup(primaryFilename) {
    alert("Inside redirect2MainLookup Function value = " + 
    primaryFilename);
    //location=primaryFilename;
    alert("Location = " + primaryFilename);
    //window.location.href = primaryFilename;
    window.location.replace(primaryFilename);
    //$(location).attr('href', primaryFilename);
};

My expectation is that ~/LookupCodes/Character.cshtml will be passed to the function for the redirect, WITHOUT /LookupCodes/Edit/ before it.

StevenJe
  • 82
  • 2
  • 14
  • 3
    `~` is a feature that only exists in server-side C#. Learn about relative URLs. – SLaks Sep 04 '19 at 18:55
  • 2
    have you tried without the `~` at the beginning? – JamesT Sep 04 '19 at 18:55
  • Thanks for heading me in the right direction. I added the following to the Javascript function: primaryFilename = primaryFilename.replace('~', ''); and now this redirects fine. – StevenJe Sep 04 '19 at 19:13
  • I should have caught that the tilde would not be recognized by Javascript, but still surprised that it prepends the previous file path to the new variable path instead of causing an error. – StevenJe Sep 04 '19 at 19:15

0 Answers0