ServiceStack.Razor CustomHttpHandler更改StatusCode

时间:2013-06-03 18:00:36

标签: servicestack

鉴于以下配置,ServiceStack将呈现notfound razor视图,并在发生NotFound错误时返回200状态代码。

如何使用RazorHandler呈现notfound视图并保留正确的404状态代码?

SetConfig(new EndpointHostConfig {
    CustomHttpHandlers = {
        { HttpStatusCode.NotFound, new RazorHandler("/notfound") },
        { HttpStatusCode.Unauthorized, new RazorHandler("/login") },
    }
});

1 个答案:

答案 0 :(得分:2)

notfound.cshtml:

@{
  this.Layout = "_YourLayout";
  this.Response.StatusCode = 404;
}