如何覆盖ServiceStack中的默认404页面?

时间:2012-08-22 10:25:46

标签: c# http-status-code-404 servicestack

我正在使用ServiceStack并拥有自己的处理程序,因为任何抛出的异常都可以很好地运行。但是,我似乎无法找到如何覆盖从ServiceStack返回的默认404错误页面。目前身体看起来像这样:

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。

我非常感谢任何有关我需要覆盖或更改的帮助或指示。如果您需要我的任何进一步细节,请随时提出,我会尽快回复。

1 个答案:

答案 0 :(得分: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() } }
});