I am creating a REST service using .NET and have used a generic handler, Default.ashx, to handle the incoming request. This allows me to access the service using methods like "http://rest/test" without issue. But when a file extension is added IIS no longer redirects the request but instead looks for a file. How can the web.config be modified so that URL request like "http://foo/test.xml" and "http://foo/test.json" are also handled by the DefaultHandler.ashx? I have done this before so know its possible but cannot remember the configuration.
You can see an example of this in my HttpClone app's web.config. The gist of it involves removing handlers for the extensions you don't want like this:
<system.webServer>
...
<handlers accessPolicy="Read, Script">
<clear />
<add name="Favorite-Icon" path="/favicon.ico" verb="GET,HEAD" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="HttpClone" path="*" verb="GET,HEAD,POST,DEBUG" type="Namespace.MyCustomHandler, AssemblyName" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
This says to use a custom handler for all requests for all URIs except '/favicon.ico'. If you still need to use the 'Default.ashx' then you will need to get handler for the ashx type and map it to handle all paths just like this. Generally though there is no need for the ashx extension handler, just implement IHttpHandler in any assembly and reference it in the 'type' attribute above.
Note that the cassini web server (the test server in VStudio) will not map the default directory '/' to you're handler. To fix this for cassini you need a default.aspx document to exist (although it can be empty).
Note 2 - The configuration above is for integrated mode only, for classic mode the concept is the same but the settings are in a different location.
Related
C#, .NetCore 2.2, Visual Studio 2019
I am having a really hard time getting used to the web.config to appsettings.json conversion process. Yes, I have read the docs but there is something I am missing.
I have been told that there is no 1:1 conversion from web.config to appsettings.json. I have read that there is nothing particularly special about the appsettings.json file and that the info there could be in nearly any other format / storage system. I have read that appsettings is just read by whatever policy provider needs to read it.
How do you know what policy providers exist or which one(s) you need?
In ny current case I have an existing, older project that uses a third party web base authentication service called "Siteminder". The use case is very simple: the older app has a small set of controllers. Siteminder is configured (server level not app level) to monitor request URLs. If a request goes to "https://thing.company.com/Auth/" then Siteminder looks for its auth token and either interrupts the request and challenges the visitor for credentials or validates and passes the request on.
My new app should work the same way.
The old app has a web.config that looks something like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*"
modules="AspNetCoreModuleV2"
resourceType="Unspecified" />
<add name="handler-wa-32" path="*" verb="*"
modules="IsapiModule"
scriptProcessor="C:\Program Files\CA\webagent\win32\bin\ISAPI6WebAgent.dll"
resourceType="Unspecified"
requireAccess="None" preCondition="classicMode,bitness32" />
<add name="CASiteMinderWebAgentHandler-fcc-32"
path="*.fcc" verb="*"
modules="CASiteMinderWebagentModule-32"
resourceType="Unspecified"
preCondition="integratedMode,bitness32" />
<!-- 10 additional, similar "CASiteMinderWebAgentHandler-???-??" handlers !-->
</handlers>
<modules>
<add name="CASiteMinderWebagentModule"
preCondition="integratedMode,bitness64" />
<add name="CASiteMinderWebagentModule-32"
preCondition="integratedMode,bitness32" />
</modules>
<isapiFilters>
<filter name="SiteMinder Agent"
path="C:\Program Files\CA\webagent\win64\bin\ISAPI6WebAgent.dll" enabled="true"
preCondition="classicMode,bitness64" />
<filter name="SiteMinder Agent-32"
path="C:\Program Files\CA\webagent\win32\bin\ISAPI6WebAgent.dll" enabled="true"
preCondition="classicMode,bitness32" />
</isapiFilters>
</system.webServer>
</location>
</configuration>
I have not found Siteminder docs on using .Net Core 2.x yet. I have no idea how to let my app, the web server, whatever, know about these settingS.
It feels like there is a missing section of documentation. How do you port this sort of config over to appsettings and let whatever systems, services, providers know about them?
SiteMinder does not provide plugins for .Net Core. Further, HTTP Modules written for IIS/.Net 4.x are not compatible with .Net Core anyway, which has a very different request processing pipeline.
I've done a fair amount of work with enabling "traditional" access managers to work with .Net Core. Its tricky. (disclaimer, shameless plug here...). My company's product provides a universal REST-based interface for use with SSO products including SiteMinder, and provides .Net and .Net Core libraries (as well as Java, C, C++, and a variety of web and app servers). You might ask your SiteMinder owners to take a look, the link is on my profile.
Looks like my best solution is to continue to use web.config for Siteminder. It works. I was hoping that there was a way around that though.
How can I configure my Asp.net project to handle all requests for a custom file extension like ".coolpix" regardless of the request path in one ashx file.
If you want an application wild solution, a way to you archieve it, is by adding a handler mapping into your web.config:
<system.webServer>
<handlers>
<add name="myExtensionHandler" preCondition="integratedMode" verb="GET" path="*.Extension" type="NameSpace.MyExtentionHandlerClass, DLLAssemblyName" />
</handlers>
</system.webServer>
Your MyExtentionHandlerClass should implement IHttpAsyncHandler or IHttpHandler
I've got an empty ASP.NET project with single HttpHandler, saved in Index.ashx. I want all requests to go through this,
public void ProcessRequest(HttpContext context)
I've modified my project project properties so that it loads that handler by default. However, if I type a different Url in the browser it won't be passed through that handler. How do I get it to do that?
What you want to do is generally achieved via HttpModule and not via HttpHandler. The module can intercept all requests and modify request/responses as per need.
...and to explicitly answer my question, you can add something like this to your web.config file (which can also be done through the IIS Manager GUI):
<configuration>
<system.webServer>
<handlers>
<add name="RequestHandler" path="*" verb="*" type="WebApplication6.RequestHandler" />
</handlers>
</system.webServer>
</configuration>
where "name" is an arbitrary name and "type" is the fully-qualified class name (with namespace).
I want to write an HttpHandler to redirect traffic to various webpages on the server.
The user will type in http://www.thisissupposedtoberedirected.com/site12 and should be redirected to the appropiate site, in this example site version 1.2
I know how to program in ASP.NET and C# but I don't seem to grab the finer detail about website management.
How can I manage to get this done? What should I do in the web.config? I've read this msdn page but it isn't much help.
HttpHandlers are actually fairly simple components.
First, you need to create a class that inherits either IHttpHandler or IHttpAsyncHandler (for your use, I'd suggest IHttpHandler since there's really no heavy lifting being done).
You then compile the DLL and drop it in the bin folder of your web application.
Now the tricky part. Deploying HttpHandlers in the web.config file is tricky since it's different between IIS6, IIS7 Integrated Mode, and IIS7 Classic Mode. The best place to look is this MSDN page:
How to: Register HTTP Handlers
IIS6
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
</configuration>
IIS7 Classic Mode
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
<system.webServer>
<add name=SampleHandler" verb="*" path="SampleHandler.new"
Modules="IsapiModule"
scriptProcessor="FrameworkPath\aspnet_isapi.dll"
resourceType="File" />
</system.webServer>
</configuration>
IIS7 Integrated Mode
<configuration>
<system.webServer>
<handlers>
<add name="SampleHandler" verb="*"
path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly"
resourceType="Unspecified" />
</handlers>
<system.webServer>
</configuration>
As you can see, each IIS configuration requires entries in slightly different sections of the web.config file. My suggestion would be to add entries in each location so that IIS configuration changes don't break your HttpHandler.
1) You need to create a new class that implements IHttpHandler or IHttpAsyncHandler (the latter being when you are very comfortable with managing your own threads). Create your logic there.
2) Then, modify the web.config so that you register your handler:
<system.web>
<httpHandlers>
<add verb="*" path="*.htm" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.ico" type="System.Web.StaticFileHandler"/>
</httpHandlers>
</system.web>
This is a sample setup in my web.config - yours may vary slightly.
Now, your HttpHandler should be registered and based on the details provided in your registration in the web.config, when you request certain URLs, you will be mapped to the Handler you created instead of the normal ASP.NET Page Handler.
Additionally, for your specific issue, I wouldn't recommend writing a HttpHandler - I would just do a DNS redirect, or do some lookup in your OnInit code to check the Host Url, and if it is one specified in your DB, you do the redirect yourself based on the configuration data.
I tried to make a .rss file in my ASP.NET application work like a .ashx and although I did everything I was supposed to, I am still getting this error:
There is no build provider registered for the extension '.rss'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
There IS a build provider registered!
<buildProviders>
<add extension="*.rss" type="System.Web.Compilation.WebHandlerBuildProvider"/>
</buildProviders>
<httpHandlers>
<remove verb="*" path="*.rss"/>
<add verb="*" path="*.rss" type="System.Web.UI.SimpleHandlerFactory"/>
...
I also added .rss in the IIS config. What is left to do?! Using ASP.NET 3.5
In the extension attribute, remove the asterisk:
<add extension=".rss" type="System.Web.Compilation.WebHandlerBuildProvider"/>
Build providers are used to generate source code at runtime. Are you sure you mean to generate source code for rss files? One thing I can say with a decent amount of certainty is there appears to be no WebHandlerBuildProvider.
Also, have you seen the RSS Toolkit?