Relative Image URL in Javascript File - ASP.net MVC and IIS 7

I am developing an asp.net mvc application, everything works fine on the ASP.net development server, but when I try to host it in IIS 7. I have a problem with URL resolution. I used relative paths in Javascript to give an image. The script file is in the ~ / Scripts / folder folder, and the image files are in the ~ / Content / images / folder. Now in the javascript file I am trying to use give, giving a relative path like http: // local / webapp1 / controller1 / action1 / it is trying to find it in http: // localhost / controller1 / action1 / and cann; t find the file.

+4
source share
4 answers

If I read this correctly, are you now using WebApp from the root of the IIS7 domain instead of WebApp1 from your development environment?

So, if my assumption is correct, then ~ / should now be allowed http: // localhost / instead of http: // localhost / WebApp1 /

If all this is still correct, your folder structure has moved one level with your ~ / scripts folder along the absolute path:

http: // localhost / Scripts /

and image folder like:

http: // localhost / Content / Images /

To access images from your scripts, you can use several methods. The easiest way is to use the relative path from your scripts directory: "../Content/Images/MyImage.jpg"

Or you can use document.location.host to create the fully qualified path name in javascript: document.location.host + "/Content/Images/MyImage.jpg"

Another method is to create ASP.NET for this part of the script dynamically, so that the full path name is entered. You can do this using ScriptManager.RegisterStartupScript or ScriptManager.RegisterScriptBlock

There really are many ways to fool this cat, they are only the first 3 that I can think of from my head.

+4
source

Try:

<a href = "~ / controller1 / action1 /" id = "testLnk" runat = "server">

The runat attribute must ensure that the infrastructure allows it. You can also use the ResolveUrl method.

+1
source

I had the same problem and found a solution to an element of David Banister. The problem is related to the action of the MVC controller index, which returns the URL in the form (www.mysite.com/viewName, unlike www.mysite.com/viewName/index), which later the web browser will fail when trying to link to css or js using relative path syntax (../../).

His solution works well for me. Hope this helps someone. Good luck.

http://blog.davidbanister.com/2010/09/28/mvc-fixes-relative-path-issues-in-asp-net-mvc-2/

+1
source

I had this problem in the past, try using tilde

~ / controller1 / action1 /

0
source

All Articles