/// <summary> /// 如果请求/asset/的文件 /// </summary> /// <param name="app"></param> /// <returns></returns> public static IApplicationBuilder UseEmbeddedAsset(this IApplicationBuilder app) { var assetList = new EmbeddedAssetProvider(_pathToAssetMap); var thisAssembly = typeof(FilterExtention).Assembly; foreach (var resourceName in thisAssembly.GetManifestResourceNames()) { if (!resourceName.StartsWith("spa.Asset")) { continue; // original assets only } var path = resourceName.Replace("\\", "/"); _pathToAssetMap[path.ToLower()] = new EmbeddedAssetDescriptor(thisAssembly, resourceName, false); } return(app.UseWhen( c => { var url = c.Request.Path.Value !.ToLower(); return url.Contains("/asset/"); }, _ => _.Run((async context => { using var webAsset = assetList.GetAsset(context.Request.Path.Value !.ToLower()); if (webAsset != null) { context.Response.ContentType = webAsset.MediaType; await webAsset.Stream.CopyToAsync(context.Response.Body); } })))); }
public void GetAsset_empty() { var prov = new EmbeddedAssetProvider( new Dictionary <string, EmbeddedAssetDescriptor>(), new Dictionary <string, string>()); Assert.Throws <AssetNotFound>(() => prov.GetAsset("", "")); }
public void GetEmbeddedResourceStreamFor_null() { var prov = new EmbeddedAssetProvider( new Dictionary <string, EmbeddedAssetDescriptor>(), new Dictionary <string, string>()); var ad = new EmbeddedAssetDescriptor(typeof(EmbeddedAssetProviderTests).Assembly, "123", false); Assert.Throws <AssetNotFound>(() => prov.GetEmbeddedResourceStreamFor(ad, "")); }
public void GetAsset_ext_empty() { var prov = new EmbeddedAssetProvider( new Dictionary <string, EmbeddedAssetDescriptor>(), new Dictionary <string, string>()); var a = prov.GetAsset("", "index/ext"); Assert.AreEqual("application/json", a.MediaType); Assert.AreEqual(2, a.Stream.Length); }
/// <summary> /// 内置管理页面路由 /// </summary> /// <param name="app"></param> /// <param name="path"></param> /// <returns></returns> public static IApplicationBuilder UseEmbeddedPage(this IApplicationBuilder app, string path) { var assetList = new EmbeddedAssetProvider(_pathToAssetMap); return(app.UseWhen( c => { var url = c.Request.Path.Value !.ToLower(); return url.EndsWith($"spa.{path.ToLower()}"); }, _ => _.Run((async context => { var authCheck = ApiMiddleware.AuthCheck(context, path); if (authCheck == null) { return; } using var webAsset = assetList.GetAsset($"/spa/asset/{path.ToLower()}/{path.ToLower()}.html"); context.Response.ContentType = webAsset.MediaType; await webAsset.Stream.CopyToAsync(context.Response.Body); })))); }
public void InferMediaTypeFrom(string file, string mediaType) { Assert.AreEqual(mediaType, EmbeddedAssetProvider.InferMediaTypeFrom(file)); }
public void GetAsset_ext_null() { var prov = new EmbeddedAssetProvider(null, null); Assert.Throws <ArgumentNullException>(() => prov.GetAsset("", "index/ext")); }
public void GetAsset_null() { var prov = new EmbeddedAssetProvider(null, null); Assert.Throws <NullReferenceException>(() => prov.GetAsset("", "")); }