How to pass all requests to handler using Visual Studio Development server? - c#

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).

Related

ASP.net handling all requests for a custom file extension in one ashx file

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

In Asp.Net 5 where does %DNX_PATH% in the wwwroot/web.config come from?

I'm trying to understand the boot process for and Asp.net 5 app running on IIS or IIS Express. My understanding so far is that when a request comes in to IIS, it runs the httpPlatform handler due to the following code specified in the wwwroot/web.config file:
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
</system.webServer>
</configuration>
So when IIS runs the httpPlatformHandler, I believe it gets it's config information from the httpPlatform tag above. This is correct right? Where does "%DNX_PATH% come from? and Where does "%DNX_ARGS%" come from? How do these place holders get their values?
If I can understand this, then I can probably get a better idea of what the next step in the process is. Thanks!
%DNX_PATH% is set when you install ASP.NET 5.
When you download the installer from http://get.asp.net, it will automatically download the first piece of the puzzle which is dnvm.
dnvm will allow you to get the latest (and previous) .NET runtime.
This runtime will be used in web.cmd to create the variable %DNX_PATH% that will then be inserted in that configuration file.
It is important to know that you do need dnx installed on this machine to run it.

Have ServiceStack default route go to MVC3 Controller

I've installed ServiceStack.Host.MVC into an existing MVC3 project that has a lot of controllers.
It's now routing to default.htm. I found another StackOverFlow question that said to change the Default redirect path, do the following.
SetConfig(new EndpointHostConfig {DefaultRedirectPath = "/Foo" });
But I really want to change it to execute the default Controller and Action as setup in the global.ascx. I've edited the DefaultRedirectPath to point at /Home, and it seems to work fine, and doesn't add Home to my url or anything, but I'm not 100% on the relationship between having an AppHost in my application.
If changing this isn't the right thing to do, what's the best way to get started integrating some Service Stack AppHost in with a regular MVC application. I'm going to move my base controllers to ServiceStackController, and slowly start using the new Session and User management stuff. Eventually, I'm going to want to use the authorization or shared sessionId with some services, but I'll get to that later.
To start, my goal is to start using some of the IOC functionality of Funq.
You probably don't need DefaultRedirectPath, because it is just a redirect to a predefined URL. You need to have your MVC controllers hit when using your old urls, and have a new URL area for ServiceStack. To do this, start by setting ServiceStackHandlerFactoryPath. It should allow you to host ServiceStack in a sub-url, and it will not interfere with your existing controllers, unless there is a conflict in the path:
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" });
The ServiceStack http handler in your web.config will need to match:
<location path="api">
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>
The ServiceStack BootstrapAPI application sets the CatchAllController to be the HomeController of the MVC application. This worked for me.
In the AppHost::Configure, I put the following code:
public override void Configure(Funq.Container container)
{
// Other Configuration constructs here
ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
}

REST with .NET using a Default Handler

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.

There is no build provider registered for the extension '.rss'

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?

Categories

Resources