Use cs file as Web handler

0

I want my ascx.cs file to work as ashx file for some reason. I have implemented System.Web.IHttpHandler into it and registered it as a handler in web.config

<httpHandlers>
.
.
.
<add path="*.cs" verb="*" type="Namespace.ClassName, Assembly.dll" validate="false" />
</httpHandlers>

And added following code in request filtering under System.WebServer in `web.config'

<security>
  <requestFiltering>
    <fileExtensions allowUnlisted="true">
      <remove fileExtension=".cs" />
      <add fileExtension=".cs" allowed="true" />
    </fileExtensions>
  </requestFiltering>
</security>

but when I open .cs file from browser, it gives me error.

HTTP Error 404.3 - Not Found

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

with following details

Module StaticFileModule

Notification ExecuteRequestHandler

Handler StaticFile

Error Code 0x80070032

Requested URL http://localhost:80/MyClass.ascx.cs

Physical Path MyPath

Logon Method Anonymous

Logon User Anonymous

c#
asp.net
iis-7.5
asked on Stack Overflow Feb 8, 2016 by Imad

1 Answer

0

You have to add a separate section for static files, see here IIS7, web.config to allow only static file handler in directory /uploads of website Its different to exactly what you want, but the static file concept is shown.

answered on Stack Overflow Feb 8, 2016 by Chris Watts • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0