3

我正在使用 ServiceStack 并且有我自己的处理程序来处理任何抛出的异常,这些异常工作得很好。但是,我似乎无法找到如何覆盖从 ServiceStack 返回的默认 404 错误页面。目前Body看起来是这样的:

Handler for Request not found: 


Request.ApplicationPath: /
Request.CurrentExecutionFilePath: /Test/123
Request.FilePath: /Test/123
Request.HttpMethod: GET
Request.MapPath('~'): C:\Test-Web\src\Web.Service\
Request.Path: /Test/123
Request.PathInfo: 
Request.ResolvedPathInfo: /Test/123
Request.PhysicalPath: C:\Test-Web\src\Web.Service\Test\123
Request.PhysicalApplicationPath: C:\Test-Web\src\Web.Service\
Request.QueryString: 
Request.RawUrl: /Test/123
Request.Url.AbsoluteUri: http://localhost:65079/Test/123
Request.Url.AbsolutePath: /Test/123
Request.Url.Fragment: 
Request.Url.Host: localhost
Request.Url.LocalPath: /Test/123
Request.Url.Port: 65079
Request.Url.Query: 
Request.Url.Scheme: http
Request.Url.Segments: System.String[]
App.IsIntegratedPipeline: False
App.WebHostPhysicalPath: C:\Test-Web\src\Web.Service
App.WebHostRootFileNames: [extensions.cs,global.asax,global.asax.cs,Web.Service.csproj,Web.Service.csproj.user,Web.Service.ncrunchproject,packages.config,servicestack.common.dll,servicestack.common.pdb,servicestack.common.xml,servicestack.interfaces.dll,servicestack.interfaces.pdb,servicestack.interfaces.xml,servicestack.redis.dll,web.config,web.debug.config,web.release.config,bin,dto,exceptions,interfaces,mongo,obj,pipeline,properties,resources,serialization,servicestack,supporting,validators,windsor]
App.DefaultHandler: metadata
App.DebugLastHandlerArgs: GET|/Test/123|C:\Test-Web\src\Web.Service\Test\123

显然是 404 StatusCode。

任何关于我需要覆盖或更改的帮助或指导将不胜感激。如果您需要我提供更多详细信息,请随时询问,我会尽快回复。

4

1 回答 1

4

默认 404 处理程序的源代码可以在这里找到: https ://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/WebHost.Endpoints/Support/NotFoundHttpHandler.cs 。

您的自定义 404 处理程序需要实现IServiceStackHttpHandlerIHttpHandler

public class YourNotFoundHandler : IServiceStackHttpHandler, IHttpHandler
{
    public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
    { ... }

    public void ProcessRequest(HttpContext context)
    { ... }
}

要覆盖AppHost.Configure()方法中的默认 404 处理程序:

SetConfig(new EndpointHostConfig {
    CustomHttpHandlers = { { HttpStatusCode.NotFound, new YourNotFoundHandler() } }
});
于 2012-08-22T10:49:11.897 回答