ServiceStack 4:在后备路由中忽略favicon.ico

时间:2014-03-27 22:03:15

标签: routing servicestack favicon ignoreroute

我在ServiceStack.Razor上构建了一个或多或少的静态网站,其路由定义为以下模式: 我试图忽略favicon.ico,但将路径如“/”或“/ en-us”路由到HomeScenario。 其他样本路线是/ {Lang} / cook或/ {Lang} / cheer等。

不幸的是,我目前的方法并没有忽略favicon.ico。我希望在没有希望编写大量额外代码的情况下实现它。

[FallbackRoute("/{Lang*}")]
public class HomeScenario : LocalizedRequest
{

}

public class LocalizedRequest
{
    public LocalizedRequest()
    {
        Lang = "en-us";
    }
    public string Lang { get; set; }
}

这是默认请求

[DefaultView("home")]
public object Get(HomeScenario request)
{
    var cacheKey = GetCacheKey ("home", request.Lang);
    return base.Request.ToOptimizedResultUsingCache (base.Cache, cacheKey, () => {
        var response = LoadJson<HomeScenarioResponse> (request.Lang, "home");
        return response;
    });
}

1 个答案:

答案 0 :(得分:1)

您可以忽略代码中的请求:

[DefaultView("home")]
public object Get(HomeScenario request)
{
    if (base.Request.PathInfo == "/favicon.ico")
        return HttpError.NotFound(request.PathInfo);

    var cacheKey = GetCacheKey ("home", request.Lang);
    return base.Request.ToOptimizedResultUsingCache (base.Cache, cacheKey, () => {
        var response = LoadJson<HomeScenarioResponse> (request.Lang, "home");
        return response;
    });
}

否则,您可以在处理不需要的请求的Request Pipeline上进一步注册CatchAll处理程序,例如:

this.CatchAllHandlers.Add((httpmethod, pathInfo, filepath) => {

    if (pathInfo == "/favicon.ico") 
        return new NotFoundHttpHandler();

    return null; //continue processing request
});