2013-07-09 41 views
2

任何默認文檔類型下面的代碼是在ServiceStack插件的初始通支持angularjs配置$locationProvider.html5Mode(true);當servicestack是自託管(如這裏請求:Routing path with ServiceStackServing default index.html page when using Angular HTML5mode and Servicestack on the backend)。我認爲我有一個很好的通用解決方案,最後一個問題(除了將其切換到符合其他插件的Feature命名約定)。手柄在servicestack Html5ModeFeature插件

我如何在我的ProcessRequest中一般處理任何受支持的默認文檔類型?現在該函數假定降價。我希望有一個更優雅的解決方案,而不是文件擴展名中的switch語句。理想情況下,我想調用一些可以繼續工作的功能,因爲隨着時間的推移會支持更多的默認文檔類型。

// This code does not yet work, and omits required methods for the sake of brevity. 
// I'll update with a link to the final plugin, once I get it working. 

Public Class Html5ModeHandler : IPlugin 
{ 
    private String pathInfo = String.Empty; 

    public void Register(IAppHost appHost) 
    { 
     appHost.CatchAllHandlers.Add((string method, string pathInfo, string filepath) => 
             Factory(method, pathInfo, filepath)); 
    } 

    private Html5ModeHandler(string pathInfo) 
    { 
     this.pathInfo = pathInfo; 
    } 

    Public Html5ModeHandler Factory(method, pathInfo, filepath) 
    { 
     String root = String.Empty; 

     // loop through catchallhandlers 
     if (EndpointHost.CatchAllHandlers != null) 
     { 
      foreach (var httpHandlerResolver in EndpointHost.CatchAllHandlers) 
      { 
       if (httpHandlerResolver == this.Factory) continue; // avoid infinite loop 

       var httpHandler = httpHandlerResolver(httpMethod, pathInfo, filePath); 
       if (httpHandler != null) 
        // only handle request if no other handler is available 
        return null; 
      } 
     } 

     if (!(GetHandlerForPathInfo(method,pathInfo, pathInfo,filepath) is NotFoundHttpHandler)) 
     { 
      // GetHandlerForPathInfo replicates most of the logic from 
      // ServiceStackHttpHandlerFactory.GetHandler and ServiceStackHttpHandlerFactory.GetHandlerForPathInfo 
      // Bail if it returns something other than a NotFoundHttpHandler 

      return null; 
     } 

     foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments) 
     { 
      var defaultFileName = Path.Combine(Directory.GetCurrentDirectory(), defaultDoc); 
      if (!File.Exists(defaultFileName)) continue; 
      root = root ? root : (String)defaultDoc; // keep the first default document found. 
     } 

     // support HTML5Mode for Single Page App - override NotFoundHttpHandler with default document 
     return new Html5ModeHandler("/" + root); 
    } 

    public override void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName) 
    { 
     // TODO: Generalize to handle any DefaultDocument type 
     MarkdownHandler handler = new MarkdownHandler(this.pathInfo); 
     handler.ProcessRequest(httpReq, httpRes, operationName); 
    } 
} 

回答

1

我已經證實我自己的滿意度,沒有簡單的解決方案。我的ProcessRequest方法目前看起來像這樣。

public override void ProcessRequest(
      IHttpRequest httpReq, IHttpResponse httpRes, string operationName) 
    { 

     if (FileFormat == DefaultFileFormat.Markdown) 
     { 
      ProcessMarkdownPage(httpReq, httpRes, operationName); 
      return; 
     } 

     if (FileFormat == DefaultFileFormat.Razor) 
     { 
      ProcessRazorPage(httpReq, httpRes, operationName); 
      return; 
     } 

     fi.Refresh(); 
     if (fi.Exists) 
     { 
      ProcessStaticPage(httpReq, httpRes, operationName); 
      return; 
     } 

     ProcessServerError(httpReq, httpRes, operationName); 

    }