简体   繁体   中英

Reach static files under application within website using IIS

I have multiple ASP.NET apps deployed within the Default Web Site in IIS (8). Their virtual directories set up like: App1, App2, so I reach them as localhost/App1/... , localhost/App2/... , they work well.

I want to load a javascript file like below in the masterpage file in the header tag in one of my apps:

<script src="/Script/myfile.js" type="text/javascript"></script>

When I open the page, I get errors in the console as my files cannot be found, because it search for the file at localhost/Script/ instead of localhost/App1/Script/

Is there a way to set the root directory to be the app's root directory containing the virtual path to the app?

I don't want to hardcore the /App1 string to the begin of the js files path, because the app is deployed more times, with different Application names under different websites, and I have many js files too.

With the example below, I had also no success:

<script type="text/javascript" src="<%# Page.ResolveClientUrl("~/Script/myfile.js") %>"></script>

But if I changed the <%# %> tag to <%= %> , then it worked on some page, but anothers threw error: "Only content controls are allowed directly in a content page that contains content controls". For this error, I found a solution to change the <%= %> to <%# %> , which not worked for me, it made my js files not working again on any page.

Any ideas?

I found that the solution using the code below works (it's in my masterpage's head tag):

<script type="text/javascript" src="<%# Page.ResolveClientUrl("~/Script/myfile.js") %>"></script>

But I need to put the code below into the masterpage's pageload method:

if(!IsPostBack)
        this.Page.DataBind();

Without this, the file won't be loaded, that's why it didn't work for me for the first time.

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM