【问题标题】:How do you override the default 404 page in ServiceStack?如何覆盖 ServiceStack 中的默认 404 页面?
【发布时间】:2012-08-17 18:11:09
【问题描述】:

我正在使用 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。

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我已经这样做了,它在我的本地机器上的 IIS express 中运行良好,但是如何让它覆盖服务器上 IIS 7 中的 IIS 404?
    猜你喜欢
    • 2011-11-08
    • 2012-10-23
    • 2022-10-12
    • 2019-08-29
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多