【发布时间】:2018-05-18 12:28:07
【问题描述】:
我将 ServiceStack (V5.1.0) 作为 Windows 服务,提供 REST API,没有问题。我想创建一个插件,为任何以 /GUI 开头的路由提供来自特定物理目录的静态文件。
我在这里 ServiceStack Razor with Multiple SPAs 阅读了“Q:ServiceStack Razor with Multiple SPAs”
但这似乎只处理像 index.html 这样的单个文件,而且我不仅需要处理物理路径根目录中的文件,还需要处理物理路径子目录中的文件。例如,路由 /GUI/css/site.css 应该提供在根目录下的 css 子目录中找到的 site.css 文件。
我在这里查看了“在 ServiceStack 中映射静态文件目录” https://forums.servicestack.net/t/mapping-static-file-directories-in-servicestack/3377/1 并基于此,尝试覆盖 GetVirtualFileSources
public class AppHost : AppSelfHostBase {
...
// override GetVirtualFileSources to support multiple FileSystemMapping.
// Allow plugins to add their own FileSystemMapping
public override List<IVirtualPathProvider> GetVirtualFileSources()
{
var existingProviders = base.GetVirtualFileSources();
// Hardcoded now, will use a IoC collection populated by plugins in the future. Paths will be either absolute, or relative to the location at which the Program assembly is located.
existingProviders.Add(new FileSystemMapping("GUI",@"C:\Obfuscated\netstandard2.0\blazor"));
return existingProviders;
}
....
}
并在插件模型中使用 FallBackRoute,
[FallbackRoute("/GUI/{PathInfo*}")]
public class FallbackForUnmatchedGUIRoutes : IReturn<IHttpResult>
{
public string PathInfo { get; set; }
}
但我不知道如何获取接口方法以将 PathInfo 更改为实现 IVirtualFile 的对象。
public HttpResult Get(FallbackForUnmatchedGUIRoutes request)
{
// If no file is requested, default to "index.html"" file name
var cleanPathInfo = request.PathInfo ?? "index.html";
// Somehow, need to convert the cleanPathInfo into an IVirtualFile, that specifies the correct VirtualPathProvider (indexed by "GUI"")
// insert here the magic code to convert cleanPathInfo into an object that implements IVirtualFile
// var cleanVirtualPathInfo = cleanPathInfo as IVirtualFile
// to make use of ServiceStack enhanced functionality, wrap the cleanVirtualPathInfo in a HttpResult,
HttpResult httpresult = new HttpResult(cleanPathInfo,false); // this doesn't compile, because no overload with 2 parameters takes a string as the first parameter, but there is an overload that will take an IVirtualFile object
return httpresult;
}
有什么建议可以让界面代码返回正确的文件吗?还是一种更好的方法来允许多个插件,每个插件都支持不同的 SPA,基于路由的第一部分?提示、链接、明确的说明 - 欢迎任何和所有!
【问题讨论】:
标签: servicestack