C# 处理servicestack Html5ModeFeature插件中的任何默认文档类型
下面的代码是ServiceStack插件的初始过程,用于支持angularjs配置C# 处理servicestack Html5ModeFeature插件中的任何默认文档类型,c#,angularjs,
servicestack,html5-history,C#,Angularjs,
servicestack,Html5 History,下面的代码是ServiceStack插件的初始过程,用于支持angularjs配置$locationProvider.html5Mode(true)servicestack自托管时(根据此处的请求:和)。我想我已经有了一个很好的通用解决方案,还有最后一个问题(除了切换到特性与其他插件一致的命名约定) 如何在ProcessRequest中一般处理任何受支持的默认文档类型?现在,函数假定降价。我希望有一个比关闭文件扩展名的switch语句更优雅的解决方案。理想情况下,我希望调用一些可以继续工作的东西
$locationProvider.html5Mode(true)代码>servicestack自托管时(根据此处的请求:和)。我想我已经有了一个很好的通用解决方案,还有最后一个问题(除了切换到特性
与其他插件一致的命名约定)
如何在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);
}
}
我已经证实,没有简单的解决办法,我自己也很满意。我的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);
}