Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a route with self-hosted ServiceStack

Tags:

servicestack

I am currently working on a solution where we have a self-hosted ServiceStack layer running, but the problem is that I keep getting errors when I access it from the browser and the browser tries to get the favicon. As far as I can see there is no option of ignoring a specific route when running self-hosted?

I would have imagined something like

Routes.Ignore("favicon*")

a bit like the

Routes.Add<Foo>("/foo")

in my AppHost Configure method

like image 579
cyberzed Avatar asked Nov 07 '12 11:11

cyberzed


2 Answers

In my web.config I like to have something like this

<handlers>
    <add verb="*" path="*.*" type="System.Web.StaticFileHandler" name="files" />
    <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/>
</handlers>

That way all files with an extension get handled by IIS and means you don't have to go all the way through the aspnet pipeline to server up a 404. It also means you don't log a load of 404s in your servicestack application.

like image 178
Antony Denyer Avatar answered Oct 31 '22 06:10

Antony Denyer


Unlike MVC which uses a Http Module to process and hijack all requests, ServiceStack is built-on ASP.NET's raw IHttpHandler interfaces. This means ServiceStack must handle any request matching the ServiceStack handler path (e.g. / or /api) by returning an IHttpHandler and isn't able to Ignore them like they do in MVC.

You can however catch and handle all unhandled requests by registering a handler in IAppHost.CatchAllHandlers, e.g:

appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
   if (pathInfo.StartsWith("favicon"))
      return new NotFoundHttpHandler();
});
like image 37
mythz Avatar answered Oct 31 '22 05:10

mythz