本文介绍了如何使用自托管 ServiceStack 忽略路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个解决方案,其中我们有一个自托管的 ServiceStack 层正在运行,但问题是当我从浏览器访问它并且浏览器尝试获取 favicon 时,我不断收到错误消息.据我所知,在运行自托管时没有忽略特定路由的选项?

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?

我会想象这样的事情

Routes.Ignore("favicon*")

有点像

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

在我的 AppHost 配置方法中

in my AppHost Configure method

推荐答案

与使用 Http 模块 来处理和劫持所有请求的 MVC 不同,ServiceStack 内置于 ASP.NET 的原始 IHttpHandler 接口.这意味着 ServiceStack 必须通过返回 IHttpHandler 来处理与 ServiceStack 处理程序路径(例如 //api)匹配的任何请求,并且不能忽略 就像他们在 MVC 中所做的那样.

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.

但是,您可以通过在 IAppHost.CatchAllHandlers 中注册处理程序来捕获并处理所有未处理的请求,例如:

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

这篇关于如何使用自托管 ServiceStack 忽略路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:12