最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - javascript file in subdirectory not loading - Stack Overflow

programmeradmin2浏览0评论

I build a simple ASP MVC4 application which uses ExtJS.

My main view has link to my main employee.js file:

<script src="app/employee.js" type="text/javascript"></script>

I'm publishing my application to server in my local network. On my development machine when I access url http:\\local-iis\holidays application is loaded correctly and displayed.
But on 3 others puters in the same network I get error, because browser can't find that js file.

My project structure looks like so:

--holidays (project name)
  +--app
     +--myapp.js
  +--Controllers
  +--Models
  +--(rest of ASP folders)

On my development machine when I access http:\\local-iis\holidays on chrome and I inspect source I see line:

<script src="app/employee.js" type="text/javascript"></script>

after I rollover that entry

I see correct path:

http:\\local-iis\holidays\app\employee.js  (I translated urlopy to holidays)

on other puters for the same page source directory name is removed (holidays)

I tried clearing cache, installing other browsers, switching to other puters but everything failed-on some puters this is working and on some it isn't.

I'm not asking for specific solution, but something to get started with.

It's my first project in MVC 4 and I don't know how should I configure my application to get those urls working.

I don't know why my application is removing that directory name (it is referring to root of my local-iis server)

I asked on ExtJS forum and they said this is probably IIS or ASP setting issue.

I build a simple ASP MVC4 application which uses ExtJS.

My main view has link to my main employee.js file:

<script src="app/employee.js" type="text/javascript"></script>

I'm publishing my application to server in my local network. On my development machine when I access url http:\\local-iis\holidays application is loaded correctly and displayed.
But on 3 others puters in the same network I get error, because browser can't find that js file.

My project structure looks like so:

--holidays (project name)
  +--app
     +--myapp.js
  +--Controllers
  +--Models
  +--(rest of ASP folders)

On my development machine when I access http:\\local-iis\holidays on chrome and I inspect source I see line:

<script src="app/employee.js" type="text/javascript"></script>

after I rollover that entry

I see correct path:

http:\\local-iis\holidays\app\employee.js  (I translated urlopy to holidays)

on other puters for the same page source directory name is removed (holidays)

I tried clearing cache, installing other browsers, switching to other puters but everything failed-on some puters this is working and on some it isn't.

I'm not asking for specific solution, but something to get started with.

It's my first project in MVC 4 and I don't know how should I configure my application to get those urls working.

I don't know why my application is removing that directory name (it is referring to root of my local-iis server)

I asked on ExtJS forum and they said this is probably IIS or ASP setting issue.

Share Improve this question edited Mar 29, 2013 at 16:27 Misiu asked Mar 29, 2013 at 15:18 MisiuMisiu 4,92922 gold badges100 silver badges209 bronze badges 6
  • The backslash is throwing me off. Do you really have this or did you mean / – Mike Christensen Commented Mar 29, 2013 at 15:20
  • @MikeChristensen which backslash do You mean? – Misiu Commented Mar 29, 2013 at 15:43
  • All of them. For example, http:\\local-iis\holidays\app\employee.js – Mike Christensen Commented Mar 29, 2013 at 15:45
  • @MikeChristensen I added screenshot to show what I mean with those backslashes. – Misiu Commented Mar 29, 2013 at 16:03
  • I don't see any backslashes in your screenshot. – Mike Christensen Commented Mar 29, 2013 at 16:07
 |  Show 1 more ment

1 Answer 1

Reset to default 8

Never hardcode urls in an ASP.NET MVC application:

<script src="app/myapp.js" type="text/javascript"></script>

Just use helpers:

<script src="~/app/myapp.js" type="text/javascript"></script>

The ~/app/myapp.js will properly go through the Url.Content helper which in turn will generate the correct url no matter where your application is hosted. For example if your application is hosted locally in IIS Express it might look like this:

<script src="/app/myapp.js" type="text/javascript"></script>

and when deployed in a virtual directory in IIS it might look like this:

<script src="/holidays/app/myapp.js" type="text/javascript"></script>

The helper will take care of it.

In Razor v1.0 (ASP.NET MVC 3) you would have to explicitly use the helper:

<script src="@Url.Content("~/app/myapp.js")" type="text/javascript"></script>
发布评论

评论列表(0)

  1. 暂无评论