Browse Source

fallback route to default document && base href replace

HOME 2 years ago
parent
commit
3eff22e96b

+ 2 - 0
FNZCM/FNZCM.BlazorWasm/wwwroot/index.html

@@ -7,6 +7,8 @@
     <link rel="icon" type="image/x-icon" href="favicon.ico">
     <link rel="icon" type="image/x-icon" href="favicon.ico">
     <title>Blazor WASM UI</title>
     <title>Blazor WASM UI</title>
 
 
+    <base href="/" />
+
     <!--<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />-->
     <!--<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />-->
     <link href="lib/bootstrap/bootswatch/bootstrap.min.css" rel="stylesheet" />
     <link href="lib/bootstrap/bootswatch/bootstrap.min.css" rel="stylesheet" />
     <link href="lib/select2/select2.min.css" rel="stylesheet" />
     <link href="lib/select2/select2.min.css" rel="stylesheet" />

+ 26 - 4
FNZCM/FNZCM.ConHost/HostProgram.cs

@@ -201,6 +201,8 @@ namespace FNZCM.ConHost
                         VirtualPath = modEnt.Key,
                         VirtualPath = modEnt.Key,
                         DisplayText = modEnt.Value.DisplayText,
                         DisplayText = modEnt.Value.DisplayText,
                         DefaultDocument = modEnt.Value.DefaultDocument,
                         DefaultDocument = modEnt.Value.DefaultDocument,
+                        EnableFallbackRoute = modEnt.Value.EnableFallbackRoute,
+                        HtmlBaseReplace = modEnt.Value.HtmlBaseReplace,
                         Files = new Dictionary<string, byte[]>()
                         Files = new Dictionary<string, byte[]>()
                     };
                     };
 
 
@@ -371,24 +373,44 @@ namespace FNZCM.ConHost
                 {
                 {
                     context.Response.Redirect("/");
                     context.Response.Redirect("/");
                 }
                 }
-                else if (requestPath.StartsWith("/module/") && pathParts.Count > 1)
+                else if (requestPath.StartsWith("/modules/") && pathParts.Count > 1)
                 {
                 {
                     var moduleKey = pathParts[1];
                     var moduleKey = pathParts[1];
                     if (Modules.TryGetValue(moduleKey, out var module))
                     if (Modules.TryGetValue(moduleKey, out var module))
                     {
                     {
                         var entPath = string.Join("/", pathParts.Skip(2));
                         var entPath = string.Join("/", pathParts.Skip(2));
-                        if (module.Files.TryGetValue(entPath, out var bin))
+
+                        void Output(byte[] bin)
                         {
                         {
-                            if (entPath.EndsWith(".js")) context.Response.ContentType = "application/javascript";
+                            if (entPath.ToLower().EndsWith(".js")) context.Response.ContentType = "application/javascript";
+                            else if (module.HtmlBaseReplace != null && entPath.ToLower().EndsWith(".html"))
+                            {
+                                //base replace
+                                var html = Encoding.UTF8.GetString(bin);
+                                var r = html.Replace(module.HtmlBaseReplace, $"<base href=\"/modules/{moduleKey}/\" />");
+                                bin = Encoding.UTF8.GetBytes(r);
+                            }
+
                             context.Response.OutputStream.Write(bin, 0, bin.Length);
                             context.Response.OutputStream.Write(bin, 0, bin.Length);
                         }
                         }
+
+                        if (module.Files.TryGetValue(entPath, out var bin))
+                        {
+                            Output(bin);
+                        }
+                        else if (module.EnableFallbackRoute && module.Files.TryGetValue(module.DefaultDocument, out var defBin))
+                        {
+                            entPath = module.DefaultDocument;
+                            Output(defBin);
+                        }
                         else context.Response.StatusCode = 404;
                         else context.Response.StatusCode = 404;
                     }
                     }
                     else context.Response.StatusCode = 404;
                     else context.Response.StatusCode = 404;
                 }
                 }
                 else if (requestPath == "/" && _defaultModule != null)
                 else if (requestPath == "/" && _defaultModule != null)
                 {
                 {
-                    context.Response.Redirect($"/module/{_defaultModule.VirtualPath}/{_defaultModule.DefaultDocument}");
+                    if (_defaultModule.EnableFallbackRoute) context.Response.Redirect($"/modules/{_defaultModule.VirtualPath}/");
+                    else context.Response.Redirect($"/modules/{_defaultModule.VirtualPath}/{_defaultModule.DefaultDocument}");
                 }
                 }
                 else if (requestPath == "/" || requestPath == "/classic-index")
                 else if (requestPath == "/" || requestPath == "/classic-index")
                 {
                 {

+ 2 - 0
FNZCM/FNZCM.ConHost/ModuleModels.cs

@@ -9,5 +9,7 @@ namespace FNZCM.ConHost
         public string DefaultDocument { get; set; }
         public string DefaultDocument { get; set; }
 
 
         public Dictionary<string, byte[]> Files { get; set; }
         public Dictionary<string, byte[]> Files { get; set; }
+        public bool EnableFallbackRoute { get; set; }
+        public string HtmlBaseReplace { get; set; }
     }
     }
 }
 }

+ 4 - 1
FNZCM/FNZCM.Core/ConfigFile.cs

@@ -47,7 +47,10 @@ namespace FNZCM.Core
     {
     {
         public bool IsDefault { get; set; }
         public bool IsDefault { get; set; }
         public string DisplayText { get; set; }
         public string DisplayText { get; set; }
-        public string DefaultDocument { get; set; }
         public string Path { get; set; }
         public string Path { get; set; }
+
+        public bool EnableFallbackRoute { get; set; }
+        public string DefaultDocument { get; set; }
+        public string HtmlBaseReplace { get; set; }
     }
     }
 }
 }

+ 3 - 1
FNZCM/FNZCM.Core/config.json

@@ -31,8 +31,10 @@
     "blazor": {
     "blazor": {
       "IsDefault": true,
       "IsDefault": true,
       "DisplayText": "Blazor WASM UI",
       "DisplayText": "Blazor WASM UI",
+      "Path": "z:/bwu",
       "DefaultDocument": "index.html",
       "DefaultDocument": "index.html",
-      "Path": "z:/bwu"
+      "EnableFallbackRoute": true,
+      "HtmlBaseReplace": "<base href=\"/\" />"
     },
     },
     "package": {
     "package": {
       //"IsDefault": true,
       //"IsDefault": true,