Images not showing on published ASP.NET MVC Website [duplicate] - asp.net-mvc

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Images in CSS not showing in ASP.NET MVC
I have already asked this but didn't receive an answer that fixed it. I'm asking again because I have tried everything I can think of and am absolutely stuck. My previous question was here: CSS images not showing in ASP.NET MVC
I have an ASP.NET MVC application with images in an ~/image/ directory. When I run it in development, the images show fine. When published, they do not show at all. I don't think it is a reference path issue because I've tried every combination of ../, ./, ~/, etc. I can think of. Neither css references, <img> or <asp:Image> tags work and all have the same problem. I've tried editing windows security on the image and all parent folders to no avail.
When I mouse over the image path in firebug, the image preview box just spins as though the image is found but can't load.
The very strange thing is that when I try to go to the image directly (www.web.com/images/image.png), I am redirected to the default log in page. The default account controller that ASP.NET MVC puts in projects is in my app, but I have not implemented any log in controls yet. So I think it's an issue with authentication. Or perhaps it is an IIS issue?
I appreciate any help you can give. I have been stuck on this for almost a week and may have to abandon images all together I can't get this sorted.

The redirect to a logon page is a usual indicator that your IIS security is set to force authentication before displaying the contents of the specified directory. The images subdirectory is one of those usually permitted to be visible even by unauthenticated users.
Add this to your web.config file under the <configuration> element to allow unfettered access to the contents of that directory:
<location path="images">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>

Check the path in firebug. Is it correct?
Are you using the helper to generate the URL? Something like:
<img src='<%= Url.Content("~/images/image.png")' />

I FINALLY figured it out. The image file in question was encrypted. Right click the image file -> properties -> advanced button on general tab -> uncheck "Encrypt contents to secure data" -> OK -> OK.
The tip off finally came when I noticed the file name was green in windows explorer. I see green file names all the time with no problems so I didn't think anything of it. Then I noticed it was the only green file in the entire web app folder. Put 2 and 2 together and it worked instantly. Thanks everyone for your help.

I reread and see that you are using iis instead of casini. Does it work in casino. If it does, then we are dealing with an iis config issue. Sometimes you need to run aspnet_regiis.exe to register asp.net with iis. Seems like a long shot since the other parts of the page are rendering. Worth a try. Here is a link with some additional info http://www.devx.com/vb2themax/Tip/18849.

Just for kicks, put some images under a different folder and try and see if you can hit them without having to authenticate, it would be good if you posted your web.config

Related

IIS caching "The layout page could not be found at the following path" error

The Problem
I have a website running in IIS. If I rename or delete one of the layout page .cshtml files under /Views/ the site immediately begins throwing following yellow screen error as expected
The layout page "_Layout.cshtml" could not be found at the following path: "~/Views/_Layout.cshtml".
What surprises me is that if I recreate or rename the file so it is exactly like it was before, the yellow screen persists. Why is this particular 500 error sticky?
I currently think that this has something to do with IIS and is specifically related to error handling. The site immediately detects that the layout page file is missing. It does not immediately realize when the file is back in place.
Thanks!
Some interesting clues
This happens on all of my sites I've tried this on so far. It isn't related to a specific site
I tried this on two websites at the same time. On one site I repeatedly and consistently refreshed the page hoping for a success. On the other, I left it alone for several minutes before checking again. The site I leave alone will resolve its problems and find the layout page on disk again. The site I continually make requests to appears to display the error indefinitely.
What I've tried
I have reproduced the problem on Umbraco websites using Umbraco's default routing as well as regular MVC pages using custom routing. The problem is the same for both.
I don't have output caching configured in IIS
When I am reproducing the yellow screen error, I am able to reproduce the error in multiple browsers, so I don't believe it is related to browser caching
I checked on the httpRuntime in the root web.config and the fcnMode is set to fcnMode="Single"
I've fiddled around with the web.config customErrors and httpErrors. Nothing I've done here has affected the problem.
I am able to reproduce the problem on websites where there is no custom code for caching. No CDN. No load balancer.
Versions
IIS: I have reproduced the problem on Windows Server 2012R2 running IIS 8 and Windows 11 running IIS 10
CMS: All of the websites I have tested on so far are Umbraco 7 sites. However, I have reproduced the problem on pages that are routed using Umbraco's out of the box routing as well as pages that are just set up using MVC and aren't leveraging Umbraco.
It appears to be part of the behavior of FcnMode="Single". See https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.fcnmode?view=netframework-4.8. It isn't an issue with caching. It is a problem with the way that the site's file change notifications (FCN) are configured.
The sticky 500 behavior on renaming files happens when I use FcnMode="Single" but not when I use FcnMode="Default".
FcnMode="Single" will result in only a single object to monitor file changes. This single object is responsible for monitoring changes to files in the main directory and sub directories.
FcnMode="Default" will result in a separate object to monitor file changes for each directory.
Umbraco sites, by default, use FcnMode="Single". This makes sense because Umbraco sites cache under very deeply nested directories in /App_Data/. This can result in so many of these monitors that it can affect the performance of the site. There is a great explanation of FcnMode and why it matters for Umbraco here: https://shazwazza.com/post/all-about-aspnet-file-change-notification-fcn/
Unfortunately, it appears that the single file monitor can miss renames of files in some cases.

ASP.NET Azure project does not appear to load newest content items

I have an ASP.NET MVC project and I am testing deployment to Windows Azure via the local emulator. I can run the project file fine in the development server but when I build the Azure project and it launches via the emulator I am having an issue with content files not being returned correctly. My internal CSS and JS files are being re-directed to the login page as if the authorization is failing; however I do not see where this auth requirement would be coming from.
Things I have already tried:
I have manually removed all the build files from both project (I have also tried the "clean" action for the solution)
I have tried removing the Azure project all together and creating a new one from the current version of my project.
I have tried clearing the local storage through the Azure storage interface.
I have verified that all my content is marked as "Content" in my ASP.NET project.
I have tried flagging all of my content items as "Copy always"
I have verified that the Static Content optional feature is checked
EDIT: I did a deploy to the web and everything works great there ... this is an emulator issue it appears. Any suggestions with that new bit of info?
You should verify your web.config just to be sure. Do you see something like this?
<system.web>
<authorization>
<deny users="?" />
</authorization>
...
</system.web>
Did you put the [Authorize] attribute on some of your controllers, or your controller base?
If it works in the cloud and in ASP.NET Development Server, I am not very sure why it doesn’t work in emulator. However I don’t think the issue is related to your application. For now, I would like to suggest you to check your IIS settings, such as applicationHost.config. Please see if there’re any authorization settings that may cause this issue(Compute Emulator uses IIS under the hook to host web roles). Please also try to host the site in a local IIS directly and see if the same issue could be encountered. If you can reproduce this issue in IIS as well, I would recommend you to consider to add a “IIS” tag to this thread, so more IIS experts will provide further suggestions.
Best Regards,
Ming Xu.
This thread has been open for a long time so I wanted to close it with what ended up being the solution.
It ended up being a bug with the emulator and the environment being used. As I mentioned, I was able to get it working when deployed. I actually tried this same situation 6 months later after updating to the latest Azure tool set and it worked fine so I am chalking this up to a bug in the emulator that has since been resolved.

Trying to set security on Controllers and Content

So I am having this issue of getting CSS files applied through the masterpage. I had another question: Can't get CSS loaded in Master page that helped me to apply the link tag correctly (or different options).
This is confirmed in Firebug where the call is coming back 302 found, but the styles are not being applied.
This question lead to another contributor pointing out possible access issues to the controller/content in my Web.config. Thus the reason for this question separate "Security" related question.
I have went through a number of full MVC app tutorials such as NerdDinner (v.1, v.2) and Pro MVC 2's not to mentioned a good bit of reading material in my pursuit to learn ASP.NET MVC - and I have not come across any details on access to sources such as /Content to anonymous users just to load css files. Nor have I read anything that MVC takes the approach that it is entirely locked down and you must open specific areas to specific roles/users/everyone OR open it all up for everyone.
So I still have the problem of not being able to get CSS rules applied even though it shows that the .css file is being found (302). But another issue is that I am seeing a second call to load the file in the console:
localurl.com/Account/Logon?ReturnURL=%2Content%2AdminViews.css
But errors with a 500.
Now I have commented out the logon URL line in my web.config "Authentication" section, and there is no route that I can see in my route dictionary.
Any thoughts on both of these related issues?
UPDATE
I found that the problem with why the redirection to the
localurl.com/login.aspx?returnurl=content/adminview.css
was that theforms authentication (in IIS) was enabled with the properties set to "Login.aspx". Changing this generated an ACL rights (yellow screen of death) when accessing the file directly
localurl.com/contents/adminview.css
I found adding users to the content directory (i.e. IUSR account) would rectify this issue.
So I now need to find what the proper way to set security. What account I should use ...etc. I did already have the IUSR_ComputerName already given access, which my understanding is the default IIS anonymous user account. So why this wouldn't be enough to access resources in the ~/contents/ directory is beyond me.
You could allow access to these resources using web.config.
<location path="Content">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

IIS not serving up .dat files

I have a ASP MVC web application that uses a plugin to load images and points for a 3d application.
When debugging with the the Visual Studio development server the images and the points are served up great...
http://i148.photobucket.com/albums/s19/littleniv/Debugging/local.png
Second image: same url but iis.png
When running in IIS 7 though the .Dat point files do not serve and produce a 404.
I've noticed the caching is marked as private in fiddler, but i don't know what this means. Can anyone help?
Cheers,
Stu
It's been a while, but I have seen a similar issue in IIS 6. IIS by default will only serve a file if it is configured to serve it based on the extension and mime type.
Go into the IIS Manager, click on the server, then open up "MIME Types" under the IIS Area. Hit "Add..." in the upper right corner (under actions), and type in your extension (".dat") and a mime type (depends on your data; maybe "application/octet-stream"?).
Once you've done that, you should be able to download the files.
I ran into this issue while trying to serve a blazor wasm application.
First try to browse the application locally on the web server. If you see an error similar to image below,
Network Tab Screenshot
You can make out that the Requested URL shows, Rejected-By-UrlScan added to the actual URL.
So the url scan is rejecting the .dat file request. To fix this url scan has to be configure to serve dat file types.
Open up UrlScan.ini (I found it in C:\Windows\System32\inetsrv\urlscan folder)
Find the DenyExtensions section and comment / remove the line starting with .dat
Now you will be able to load .dat files.
Hope, this answers helps someone.
OK. Apparently the 404.3 50 error is ERROR_NOT_SUPPORTED. Can anyone shed any light?
ALL STOP!!!
I added a static file handler mapping and everything is solved. Many thanks to Chris for helping me run through some trouble shooting. I'll mark you as the answer as you are the only one that helped!! Thanks again!!
Eek. I have multiple accounts on SO... this is going to take some sorting out!
For an Azure web app, the following change to the web.config did the trick, thanks for the Mime type clue:
<system.webServer>
<staticContent>
<remove fileExtension=".dat" />
<mimeMap fileExtension=".dat" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>

ASP .Net MVC - Images not being shown in published build

I am developing an ASP .Net MVC application and on my dev machine, the application runs as expected and, more importantly, the images mentioned in the CSS file are displaying correctly too.
However, when I publish this application to a testing server, the web app runs fine, but the images are not shown.
If I modify the URL in IE when testing the output from the test server, the image is returned, meaning that the file is there but it just won't appear within the view page when using the site normally.
I have tried alternative servers too, but the result is the same.
To confirm, here's a line from the CSS page referencing the image...
background-image: url('/Content/Images/Logo/myLogo.jpg');
Any suggestions?
Cheers
Brett
The URLs are not correct, likely due to the fact that you are publishing in a subfolder and so they are no longer at the root of the server. I usually use Url.Content( "~/Content/Images/..." ) to build the url instead of hard-coding it. That way it will take into account the routes when building the path.
Example:
<img src='<%= Url.Content( "~/Content/Images/banner.jpg" ) %>' alt="Banner" />
I had the same issue, but I found the reason why it was forcing authentication on the Contents folder.
When a user is not logged in yet, they are classified as Anonymous Authentication. In IIS7 (which is what I am using, guessing it is the same in IIS6) you need to open the authentication window in features view. Then edit the Anonymous Authentication, to use your application pool identity, or the default one, just make sure that user has permissions to read in that folder.
That fixed it for me, hope it works for you.
Possible relative paths are wrong...Possible that they are wrong for CSS file itself. You can use FireBug to see if CSS loaded correctly, then you can examine image request, often in such situations you will see red(error) items. This could help to localize problem.

Resources