0

This is the url which i am trying to hit from within .js file which contains knockout related function:

 self.followAction = $.resolvePath("/People/Follow?uid=" + data.UserId);

here People is the controller and Follow is the action method, on button click, i want to send userId along so i have written this.

To resolve relative path from within javascript, i have written this function

// Fix for resolving relative paths from within js scripts
 $.resolvePath = function(url)
 {
  var path = '@Request.ApplicationPath';
  if (path != '/') return path + url;
  return url;
  };

But, on button click, i am getting this error: HTTP Error 404.0 - Not Found and url is:

 localhost:44305/People/@Request.ApplicationPath/People/Follow?uid=8

please tell me what should i try now. thnks in advance!

4
  • 1
    Razor code is not interpreted within JS files, hence the @Request.ApplicationPath is read as a literal string. You need to put that code somewhere where it will be executed so that your JS can read it; as a data-* attribute on an element in your View, for example. Commented Jan 7, 2016 at 8:03
  • okk thnk u got it @RoryMcCrossan Commented Jan 7, 2016 at 8:11
  • Glad to help. I added it as an answer for you Commented Jan 7, 2016 at 8:16
  • My problem is still not solved please someone help me @RoryMcCrossan Commented Jan 7, 2016 at 9:07

1 Answer 1

3

Razor code is not interpreted within JS files, hence the @Request.ApplicationPath is read as a literal string. You need to put that code somewhere where it will be executed so that your JS can read it; perhaps as a data-* attribute on an element in your View, something like this:

<!-- in a layout view... -->
<body data-app-path="@Request.ApplicationPath">
$.resolvePath = function(url) {
    var path = $('body').data('app-path');
    if (path != '/') 
        return path + url;
    return url;
};
Sign up to request clarification or add additional context in comments.

3 Comments

i have tried putting that code in a partial view and call that partial view directly on view page but still it is saying $.resolvepath is not a function and in your answer what is data-app-path and where to put code in layout i think it should be in head section @Rory McCrossan
It is not working i dont know why what should be done
You would probably be best to start a new question about it, including the actual HTML which is output and the value of @Request.ApplicationPath

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.