2

$locationProvider.html5Mode(true);以下のコードは、ServiceStackが自己ホストされている場合に angularjs 構成をサポートする ServiceStack プラグインでの初期パスです(ここで要求されているように: ServiceStackを使用したルーティング パスと、バックエンドで Angular HTML5mode および Servicestack を使用する場合のデフォルトの index.html ページの提供)。Feature最後に 1 つの質問があります (これを他のプラグインに合わせ た命名規則に切り替えること以外は)、一般的な解決策が得られたと思います。

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);
    }
}
4

1 に答える 1

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);

    }   
于 2013-07-10T19:24:05.437 に答える