Using img tag in View to display static picture - asp.net-mvc-5

I'm moving from ASP.NET to MVC and having trouble displaying a simple image. I have a Controller:
Public Class LinkedImageController
Inherits Controller
' GET: LinkedImage
Function Index() As ActionResult
Return View()
End Function
End Class
and a View:
<img src="~/Views/LinkedImage/grey_336x280.gif" alt="Views-LinkedImage" /><!-- Doesn't work -->
<img src="~/Content/LinkedImage/grey_336x280.gif" alt="Content-LinkedImage"/><!-- Works -->
I have copies of the image in the Views/LinkedImage folder and the Content/LinkedImage folder and both are set to copy to the output folder.
As you can see from the comments in the View only the <img... tag that links to the file under the Content folder displays the image correctly.
While this is only a single image where I'm trying to get to is a design for displaying existing html questionnaires within an MVC framework so I have to work out where the various static files can be stored.
I'm new to MVC so I'm assuming that the image in the View folder cannot be displayed because the routing affects it somehow. Is this correct? If not, why does only one image display?

This line in the Web.config of your Views folder blocks direct access to files therein:
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Best policy is to put your static content in ~/Content (or another folder you create in the application root directory, if you wish) - you don't want to risk giving a malicious user access to the code in your views.

Related

Kentico sitemap not rendering with XML extension

I've gone through the documentation, set a page with the correct webpart, checked the web.config, and my sitemap only renders out the XML with .aspx.
here's my web.config snippet. I did make any changes here.
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
<remove name="XHtmlModule" />
<remove name="CMSApplicationModule" />
<add name="XHtmlModule" type="CMS.OutputFilter.OutputFilterModule, CMS.OutputFilter" />
<add name="CMSApplicationModule" preCondition="managedHandler" type="CMS.Base.ApplicationModule, CMS.Base" />
</modules>
I even tried an alias on the portal page.
In your settings you can set this. Go to Settings>URLs and SEO>Search engine optimization (SEO) and the first 2 boxes are what you want to set. The first box (Google sitemap URL) is the actual URL you'll navigate to. The second box (Google sitemap path) is where in the content tree the Google sitemap URL will get the content from.
So to say it a different way, the path is the holder of the actual sitemap content. While the URL is where you will actually navigate to to get the sitemap.
For instance if you have sitemap.xml in the Google sitemap path and your actual sitemap is located in the content tree at /special-pages/google-site-map you'd never navigate to the /special-pages/google-site-map page, only the /sitemap.xml page and that is what you'd submit to the search engines as well.

web.config causes HTTP 500 issue with virtual directory in IIS

I'm fairly new to IIS so apologies if this is a basic question.
I have an IIS config serving an internal company website (php instead of asp.net). The prod version of the website is at the 'Default Web Site' level and I've got demo and test versions of the website mapped as virtual directories. The demo and test version are essentially copies of the prod directory. I've noticed the with the web.config copied to these VDs, I get an error 500 on the root url for the VD only. I.E. main website is https://mainwebsite.com and works fine but https://mainwebsite.com/demo/ doesn't work while https://mainwebsite.com/demo/index.php works fine.
The web.config file is pretty basic:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
</handlers>
<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:03:00" />
</staticContent>
</system.webServer>
</configuration>
Moving the web.config file out of the way in the VD resolves the issue. Even though the files are identical, I wouldn't think that the file should cause a conflict as my understanding is that IIS supports multiple web config files.
Although I have a workaround in place by renaming or deleting the file, I am wondering if there's a way to keep the file in place without it causing this error.
Thanks to Panama Jack in the comments, I was able to resolve my issue.
I got this response with detailed errors:
Error Summary
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
And further down:
Config Error
Cannot add duplicate collection entry of type 'add' with unique key attribute 'value' set to 'index.php'
To resolve, I simply commented out this line in the web.config XML:
<add value="index.php" />
I'm sure there's a better way to approach this but for now, this gets me my answer and also how to get more info from IIS when the logs are not useful.
if you create a virtual directory to another web root
web.config will cause this (personally I think the location of this file is totally insane.. mixed with htm and images etc. )
Replicate the directory somewhere else without the web.config file/excluding it..then point the virtual directory there.. & have a task set up to copy newer files over..

Importing google polymer in Orchard cms

I am trying below
RegisterLink(new LinkEntry { Rel = "import", Href = Url.Content("~/Themes/SomeTheme/components/font-roboto/roboto.html") });
in my orchid layout, for using polymer. however this request is throwing a 404(on network tab in chrome.).
I am very much new to Orchid cms and hence struggling with this. Any help on this.
Note: I found that I have a class ResourceManager which implements IResourceManager. I am not sure if this is where I need to do make some changes. So adding this note as well.
Thanks
You need to place a web.config inside Themes/SomeTheme/components folder (same as ThemeMachine/Styles) making one modification:
<handlers accessPolicy="Script,Read">
<add name="StaticFileHandler" path="*" verb="*" modules="StaticFileModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
</handlers>
Remove (if existing) all web.config files in children folders.
You really need a web server to host polymer assets unless you disable web security. It can be any web server server. In addition, the ~ directive probably wouldn't be recognized by chrome file loader.

mvc 5 controlling authorization for entire controller or entire view folder

I have a mvc 5 application with a product controller and a corresponding view folder for all product related views. I'd like to make this entire thing only accessible to user with username "admin" instead of manually assigning a tag [Authorize(Users = "admin")] on top of every controller method. I tried to add a new web.config in Product views folder with the following content, but didnt work.
<configuration>
<system.web>
<authorization>
<allow users="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
Is this possible to achieve at all if yes how?
thanks in advance
The AuthorizeAttribute can be added both on a specific method and on the controller class. The second will affect every action method in that controller.
[Authorize(Users = "admin")]
public class AdminController
{
public ActionResult Index() { ... }
}
If you want to use Web.config you must remember that IIS will load the Web.config file based on the URL without any knowledge of Views. So if the URL is http://server/Products/Something then you must place your configuration file in a folder called Products in the application root. Alternatively you can use <location path="/Products"> in your root Web.config file.

iis only Add Expires headers to images

Add expires headers in iis is very Easy,but this cache all the static files. now i want only
add expires headers to images,how can i do that? even i want cached specific file?
put all your images in one folder
enter the manager--> yoursite--> images folder (or specific file)
right click--> HTTP HEADERs--> Set expire header/date ! --> APPLY/OK
I've been searching for a simpler solution and I found this.
Keep your static content inside a folder (eg: css, js). Create a web.config file inside that folder. Add these following lines. Here 7 is the number of days, change it as you desire.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
You are free to keep as many static content folder as you want, simply add this web.config file. Hope this helps.

Resources