How to ignore a route with self-hosted ServiceStack

冷暖自知 提交于 2019-11-30 09:36:56
mythz

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();
});

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.

Just to append to @antonydenyer's answer. His solution seems to work also when combining owin with servicestack3.

<handlers> <add path="auth/*" name="Microsoft.Owin.Host.SystemWeb" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" /> <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" /> </handlers>

Here SS is handling every request except /auth. Auth is mapped to Identityserver3 using owin.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!