Using HttpHandler (ashx) to serve images - image

I'm trying to implement an httphandler to serve images. I've seen plenty of examples and the process seems simple enough. However, it seems that my image tag
<img src="ImageHandler.ashx?picture=moon.jpg" />
persists in interpreting the call to the handler as a straight URL. ImageHandler.ashx is in the App_Code folder. I figure the problem is in registering the handler. Here's my current web.config entry (I've tried lots of them, includeing *.jpg as the path.):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="ImageHandler" path="*.ashx" verb="*" type="ImageHandler" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" />
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
What am I missing?!
Thanks!

Visual Studio decided that the ashx file needed to be TWO files. I knew that was going on. However, what I didn't know (until I tried copying the files into and out of the App_Code folder) was that having the ashx file in the app root and the ashx.vb file in the App_Code folder would make the handler work - without needing to register it in the web.config file. Never seen anything like it.

Related

web.config file not being read by Google PageSpeed Insights

I have read through answers here and still stuck: IIS7 Cache-Control
I have the following web.config.xml file in the root directory of my website:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00"/>
</staticContent>
</system.webServer>
</configuration>
The purpose of this web.config file is to pass the Google PageSpeed Insight 'leverage browser caching' test. I am using Windows Plesk hosting, and therefore cannot use a .htaccess file for this.
No matter how I try and format the contents of the web.config file, Google does not seem to recognise any form of browser caching is occurring. I am not sure if it is just Google, or if it means that the images and other static resources on my page are being cached or not. Is there an easy way to check this?
Can anyone see any issues with my web.config.xml contents that might be causing the issue? Or is there anything else I need to do with it other than stick it in the root directory of my site?
The file name should be web.config and not web.config.xml
*.config is already a xml type of file

Prevent redirect to site.mobile.master

When I run the web form template of Visual Studio 2013 on a mobile phone the master page is redirected to Site.Mobile.master by default. Is there any way to prevent this and how???
THANKS
you can fool the application adding the code below in your web.config inside
<system.web>
<browserCaps>
<result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<filter>isMobileDevice=false</filter>
</browserCaps>
</system.web>
The code should be placed within the <system.web> tag. Your web.config file may already contain the tag, so can be placed in the existing one.

Downloading ISO file

When i try to download iso file, it results in 404 page not found. The link in this instance is like:
http://www.domain.com/virtualDir/filename.iso
virtualDir points to a location on our file servers.
I don't want to read the binary array and then push it out as download since it is almost 600mb. I want the browser to handle this just like exes and zips.
What is the best way to handle this?
I would imagine it is because IIS doesn't serve unknown file extensions. You need to add the MIME type to the overall IIS settings, or the web.config. As you asked for in the comment, I'd probably recommend placing it in the web.config since it doesn't require additional configuration from a sysadmin and everything is in one place.
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".iso" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>

Why Do we need to change the handlers in web.config while using the telerik controls

I have a question, as on why do we need to add handelers to web.config file while working with telerik controls
what is the significance of the following code?
<system.web>
<httpHandlers>
<add verb="GET,HEAD" path="asset.axd" validate="false" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" />
</httpHandlers>
</system.web>
and another question is that in the section why do we remove the asset handler first and then immediately add it again?
<handlers>
<remove name="asset" />
<add name="asset" preCondition="integratedMode" verb="GET,HEAD" path="asset.axd" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" />
</handlers>
.axd files are HTTP handler files and Telerik probably use them for managing their scripts and assets such as images and stylesheets for their skins.
This handler has to be registered in the web.config so it's executed when the browser requests this file and to ensure it's directed to the approperate HTTP handler. Telerik controls behind the scenes can then safely assume the assets are available.
If you view the generate html source of your application you'll probably see references to asset.axd?blah==3dfijefi if you view the contents of this file you'll see exactly what's going on (although probably minified).
As for why they suggest removing and adding again I suspect it's to stop parent web.config files that may reference older versions etc... ? Just a safety net really.

MVC 3 Won't Serve Content files from Areas subfolder

I have an MVC3 application with a couple of areas and a portable area as well (using MVCContrib)
Normally, I keep all my content files under ~/Content and my scripts under ~/Scripts.
However, I am building a fairly complex webclient to another service on my site and I want to organize those javascript and image files (LOTS of image files and resources) under the Area's folder structure, which looks something like this, under ~/Areas/WebClient
Content
css
fonts
images
js
Controllers
Models
Views
I have a resource aggregator controller (one of my portable areas) that is able to reach into the CSS/JS folders just fine to provide that content. However, the CSS files reference the images/fonts folders directly and all of those links show up broken. I have double and triple checked the paths and made sure everything was right but I still get 404 errors.
As far as I know MVC3 is supposed to ignore routing so long as there's a static file there. Also, as far as I know, only the App_* folders enjoy special protection. What am I missing? I'd rather not mix in my images and resources with my main application if I can at all avoid it.
As an example: http://localhost/Areas/WebClient/Content/images/knownimage.png will not work, but should, as it exists!
So after some sleep and, more importantly, stepping away from the problem I remembered that MVC does in fact offer you protection from people downloading views directly, which led me to remember the Web.config file required in the Areas folder. Sure enough, there's an httphandler that basically sends all requests to the FileNotFound handler.
All I had to do was drop a web.config file in the content folder I wanted to expose with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler" />
</handlers>
</system.webServer>
</configuration>
Problem solved.

Resources