Y2theZ
Y2theZ

Reputation: 10412

incorrect file path when deployed to server

I am working on an asp.net mvc3 application

I want to use some javascript library in a view. I include it this way

<script type="text/javascript" language="javascript" src="../../Scripts/UserScript.js" ></script>

Locally it works. However when I deploy on the server the script file cannot be found.

I did some researches and found out that it is using the url as: Root/Scripts/UserScript.js while it should use Root/MyApplicationName/Scripts/UserScript.js

How can I make it use the correct path? Logically it should work since ../../ gives us the relative path. Why is it starting from the root?

I also checked the build on the server and it has the same architecture as the one locally (same folders tree).

Btw this is the same for any file (all the pictures, scripts and css that start with ../../)

Any help is greatly appreciated

Upvotes: 1

Views: 442

Answers (2)

Josh
Josh

Reputation: 10624

Can you use Razor syntax and locate it that way?

<script type="text/javascript" language="javascript" src="@Url.Content("~/Scripts/UserScript.js" )" ></script>

Upvotes: 0

Ofer Zelig
Ofer Zelig

Reputation: 17508

Locally your web server (IIS or Cassini) uses a virtual application, so your site's root isn't, say, http://localhost/ but rather http://localhost/MyApplicationName.

On the other side, on your production server it's a full IIS installation where your web site is defined as a root application (web site). That's why locally you have extra directory level.

Best practice, in my opinion, is to install full IIS (or IIS Express) on your dev machine and define your site as a root application (web site), so you'll program against an environment which will be similar to the one you are deploying on.

Upvotes: 3

Related Questions