Share via


MVC path issue when app running in vitual folder

Question

Wednesday, June 8, 2011 9:15 PM

I have create app that running in virtual folder which

http://{domain}/{virtualfolder}/

when in view i place
./myfile.xxx

it go to 

http://{domain}/myfile.xxx

I have to use the server side tag @Url.Content() to solve the path,
but when I need to dynamic load a file, like javascript, specially Flash, it cannot resolve the path

any way to make the relative path stick with the virtual folder as root rather than the domain?

thank you very much

All replies (12)

Saturday, June 11, 2011 12:44 AM âś…Answered

as I said, MVC has nothing to do with paths. the browser interprets relative paths and decides how they work and it based on the unix relative path rules. to a browser

  /path

means start at the root (domain, first /)

  ./path or path 

means start from the current base path (up to the final /). here is where skiping the trailing / in a url causes problems:

 http://mysite.com/foo

to the brower foo is a filename, and not part of the base path. but iis, if it does not find a file named foo, but does find a vdir names foo, it will pass the request to the vdir (which your mvc is catching) which will then return the default document (but the browser still thinks the current h is http://mysite.com/ 

you can config iis to not look in the vdir so http://mysite/foo will return file not found, or you can drop a file named foo in the root that does a redirect to /foo/ 

to get around this its common for site to have domain.com/images, domain.com/scripts, etc.

Url.Content() looks at the url sent by the browser, and applies the same rules the browser would to get the proper path. you can also write javascript to do this, but its a pain.

 

 


Thursday, June 9, 2011 1:08 AM

but when I need to dynamic load a file, like javascript, specially Flash, it cannot resolve the path

Please show code. Usually the generating HTML code is relative( i.e. /...)


Thursday, June 9, 2011 4:11 AM

its strange, just found that the flash app load resource(external files) correctly in

  • Google Chrome
  • Opera
  • IE 8
  • Safari

but not in FireFox 

<img src="./Content/images/test.png" />

in HomeController Index.cshtml

In fireFox... the image is not showing, and when using Firebug to trace the resources it load
GET http://my.domain/Content/images/test.png
should actually http://my.domain/myapp/Content/images/test.png


Thursday, June 9, 2011 6:24 AM

ok.. me found whats the cause problem, but I do know if can fixe or not

relative path : ./Content/images/test.png

when enter the url at browser as below
http://my.domain.com/myapp
it will be
http://my.domain.com/Content/images/test.png 

when enter the url as below
http://my.domain.com/myapp/
it will be
http://my.domain.com/myapp/Content/images/test.png  


Thursday, June 9, 2011 8:18 AM

use

Url.Content

see

http://msprogrammer.serviciipeweb.ro/2010/10/09/five-common-mistakes-for-asp-net-mvc-accesing-resources-css-js-images-ajax/


Thursday, June 9, 2011 8:32 AM

In Flash cannot use Url.Content 

And i feel is not a good idea to using Url.Content any where,
specially for layout designer


Thursday, June 9, 2011 8:39 AM

In Flash cannot use Url.Content 

It's a  Flash issue of relative path or a MVC one ?


Thursday, June 9, 2011 8:59 AM

inside flash is using relative path...
the flash work with standard asp.net application
but one we use it with mvc, it cannot locate the files,
and we try to trace it, the result is as my post above

it goes to root of domain instead of the virtual folder 


Thursday, June 9, 2011 11:11 AM

this has nothing to do with mvc, and all to do with realative paths. @Url.Content was invented to get around these problems. 

there is no relative path that say go to root of my vdir (as the browser has no idea there is one).

./mypath is relative path meaning to start at the current path without filename. if all your pages are all one level deep, you can use ../content/, but the default page http://<domain/<vdir> will not work, so you need to add a redirect to http://<domain>/<vdir>/home/ (also in the case the trailing / is missing). 

you can create a corporate image and css folders at the root and use /images and /css (a common approach).

 


Thursday, June 9, 2011 5:31 PM

you can create a corporate image and css folders at the root and use /images and /css (a common approach).

This is will not work with virtual directory because when url starts with slash then resource getting from root directory of domain.

codetale, you should use correct relative urls.
for example you have next structure:
http://domain.com/virtDir/images/image1.png
http://domain.com/virtDir/flash/flash.swf

In this case from flash you should use nex url to image: "../images/image1.png" (in words: step out from "flash" directory and go to "images" directory)

Second solution:

For example in MasterPage you can specify hidden field with value of virtual directory:

<input type="hidden" id="virtDir" value="<%= Request.ApplicationPath %>" />

So this hidden field should exists on all pages that inherits from that MasterPage

And now in all javascript, flash content you can use next syntax to generating urls:

document.getElementById("virtDir").value + "/Images/image1.png";

Hope that helps.


Friday, June 10, 2011 9:28 PM

you can create a corporate image and css folders at the root and use /images and /css (a common approach).

This is will not work with virtual directory because when url starts with slash then resource getting from root directory of domain.

codetale, you should use correct relative urls.
for example you have next structure:
http://domain.com/virtDir/images/image1.png
http://domain.com/virtDir/flash/flash.swf

In this case from flash you should use nex url to image: "../images/image1.png" (in words: step out from "flash" directory and go to "images" directory)

its a bit headache about the path issue

now i have to force all action with end slash if the action will return a view that load Flash
if without end slash the relative path are not working for Flash.

I just wondering how MVC rewrite the path, why
http://www.mydomain.com/myapp => http://www.mydomain.com/

and

http://www.mydomain.com/myapp/ => http://www.mydomain.com/myapp

it seem always obmit the value after the last end slash


Saturday, June 11, 2011 12:55 AM

oic..

me guess me now have the concept,

thank you very much for the details