1

So i have three files.

html has:

<script type="text/javascript" src="~/Scripts/custom-notebook.js"></script>

custom-notebook.js line gives a 404 not found:

$.getScript('Scripts/custom-notebook-services.js'); //this gives 404

1. custom-notebook-services.js exists. It used to work but I changed html pathing to include the ~/ because I have a virtual application (yes MVC C#)..

2. Now nothing I do works (yes also searched around).

  • I have tried $.getScript('../Scripts/custom-notebook-services.js');

  • and $.getScript('../../Scripts/custom-notebook-services.js');

  • and $.getScript('/Scripts/custom-notebook-services.js');

No luck:

Always gives me a 404 with path

domain.com/Controller/Scripts/custom-notebook-services.js

...instead of proper path:

domain.com/Scripts/custom-notebook-services.js

9
  • Try using the ~ in other links too. See how that works out Commented Apr 28, 2015 at 17:01
  • wierd is showing 404: domain.com/Controller/~/Scripts/custom-notebook-script.js . Commented Apr 28, 2015 at 17:09
  • 1
    Try it as <script src="@Url.Content("~/Scripts/custom-notebook-script.js")"></script> Commented Apr 28, 2015 at 17:10
  • 1
    Then the ~ won't work there Commented Apr 28, 2015 at 17:14
  • 1
    ahh. excuse me. I see what you are saying. leave the path in the html and grab it. problem is not matter what I do it includes the Controller in the url...... unless I call the entire domain, which would work. Commented Apr 28, 2015 at 17:44

2 Answers 2

4

One way to get the path from the Razor helper into your script is through a data attribute.

<body data-script-dir="@Url.Content("~/Scripts")">

Now extract that in your javascript but you'll need to delay your script until DOM loaded.

$(function() {
    var scriptDir = $("body").attr("data-script-dir");   //  "/Scripts"
});

then you can append the rest of your path to that string.

Sign up to request clarification or add additional context in comments.

1 Comment

I am upvoting Jasen's, this may be close.... you would probably need to concatenate a full domain url into the data-script-dir using the method above and building the path using stackoverflow.com/a/21226409/3023642 . For my purpose, I used a work around. This is a very strange problem! jQuery seems not to play well with MVC virtual directories.
2

You have the virtual directory well configured on IIS? On the same directory you have the page that contains this <script type="text/javascript" src="/Scripts/custom-notebook.js"></script>, you should have a virtual directory pointing to "Scripts" on that same directory of the page.

1 Comment

Right, so your saying Script dir should be in the virtual directory root yes? That is what I am doing.

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.