在Razor View Engine.net core中呈现Google图表
直到最近,我还在用谷歌图表运行一些pdf报告,突然谷歌图表就不会出现了。报告的其余部分工作正常。我使用razor视图引擎运行一个模板文件,该模板文件包含加载google图表的javascript 模板文件有一个由在Razor View Engine.net core中呈现Google图表,razor,charts,google-visualization,razor-2,dinktopdf,Razor,Charts,Google Visualization,Razor 2,Dinktopdf,直到最近,我还在用谷歌图表运行一些pdf报告,突然谷歌图表就不会出现了。报告的其余部分工作正常。我使用razor视图引擎运行一个模板文件,该模板文件包含加载google图表的javascript 模板文件有一个由触发的initCharts()函数。此函数加载Google图表,如下所示: public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
触发的initCharts()
函数。此函数加载Google图表,如下所示:
public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
dictionary.Add("LogoUrl", logoUrl);
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider, Items = dictionary };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
QSslSocket: cannot resolve CRYPTO_num_locks ] 10%
QSslSocket: cannot resolve CRYPTO_set_id_callback
QSslSocket: cannot resolve CRYPTO_set_locking_callback
QSslSocket: cannot resolve sk_free
QSslSocket: cannot resolve sk_num
QSslSocket: cannot resolve sk_pop_free
QSslSocket: cannot resolve sk_value
QSslSocket: cannot resolve SSL_library_init
QSslSocket: cannot resolve SSL_load_error_strings
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv23_client_method
QSslSocket: cannot resolve SSLv3_server_method
QSslSocket: cannot resolve SSLv23_server_method
QSslSocket: cannot resolve X509_STORE_CTX_get_chain
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
QSslSocket: cannot resolve SSLeay
QSslSocket: cannot call unresolved function CRYPTO_num_locks
QSslSocket: cannot call unresolved function CRYPTO_set_id_callback
QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback
QSslSocket: cannot call unresolved function SSL_library_init
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function sk_num
QSslSocket: cannot call unresolved function SSLv23_client_method3%
QSslSocket: cannot call unresolved function SSL_library_init
Warning: Failed to load https://www.gstatic.com/charts/loader.js (ignore)
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function SSL_library_init
函数initChart(){
load('current',{'packages':['corechart']});
google.charts.setOnLoadCallback(drawCharts);
}
用于加载各种图表的其他函数
然后,razor view引擎将加载此模板文件,如下所示:
public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
dictionary.Add("LogoUrl", logoUrl);
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider, Items = dictionary };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
QSslSocket: cannot resolve CRYPTO_num_locks ] 10%
QSslSocket: cannot resolve CRYPTO_set_id_callback
QSslSocket: cannot resolve CRYPTO_set_locking_callback
QSslSocket: cannot resolve sk_free
QSslSocket: cannot resolve sk_num
QSslSocket: cannot resolve sk_pop_free
QSslSocket: cannot resolve sk_value
QSslSocket: cannot resolve SSL_library_init
QSslSocket: cannot resolve SSL_load_error_strings
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv23_client_method
QSslSocket: cannot resolve SSLv3_server_method
QSslSocket: cannot resolve SSLv23_server_method
QSslSocket: cannot resolve X509_STORE_CTX_get_chain
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
QSslSocket: cannot resolve SSLeay
QSslSocket: cannot call unresolved function CRYPTO_num_locks
QSslSocket: cannot call unresolved function CRYPTO_set_id_callback
QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback
QSslSocket: cannot call unresolved function SSL_library_init
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function sk_num
QSslSocket: cannot call unresolved function SSLv23_client_method3%
QSslSocket: cannot call unresolved function SSL_library_init
Warning: Failed to load https://www.gstatic.com/charts/loader.js (ignore)
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function SSL_library_init
公共异步任务RenderToStringAsync(字符串视图名称、对象模型、字符串logoUrl)
{
字典=新字典();
添加(“LogoUrl”,LogoUrl);
var httpContext=newdefaulthttpcontext{RequestServices=\u serviceProvider,Items=dictionary};
var actionContext=new actionContext(httpContext,new RouteData(),new ActionDescriptor());
使用(var sw=new StringWriter())
{
var viewResult=_razorViewEngine.FindView(actionContext,viewName,false);
if(viewResult.View==null)
{
抛出新ArgumentNullException($“{viewName}与任何可用视图都不匹配”);
}
var viewDictionary=new ViewDataDictionary(new EmptyModelMetadataProvider(),new ModelStateDictionary())
{
模型
};
var viewContext=新的viewContext(
行动背景,
viewResult.View,
查看字典,
新的TempDataDictionary(actionContext.HttpContext,_tempDataProvider),
西南,
新的HtmlHelpOptions()
);
等待viewResult.View.RenderAsync(viewContext);
返回sw.ToString();
}
}
razorview引擎的输出(来自RenderToStringAsync
函数)被提供给DinkToPdf库以转换为pdf
我遇到的问题是,当执行google.charts.load
line时,它会失败,或者在加载javascript之前,razorview会返回html。javascript不会在razorview中执行,因此不会呈现图表。但是如果我将输出复制到一个HTML文件,并用浏览器打开该文件,图表就会按预期工作。在google.charts.load
行之后,javascript似乎不会在razorview引擎中执行
我是否可以在razorview引擎中看到执行javascript的结果?如果有错误,我可以看一下吗?我可以加载像google这样的第三方脚本并在razorview引擎中执行它吗
我花了很多时间都没有用。非常感谢您的帮助 通过使用PhantomJS替换DinkToPdf来创建PDF解决了这个问题。DinkToPdf无法加载Google图表。这可能是由于Google将对jsapi的请求重定向到其较新的loader.js,而DinkToPdf由于某种原因无法处理这些请求 在命令行上运行DinkToPdf使用的wkhtmltopdf显示访问https时出现以下错误:
public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
dictionary.Add("LogoUrl", logoUrl);
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider, Items = dictionary };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
QSslSocket: cannot resolve CRYPTO_num_locks ] 10%
QSslSocket: cannot resolve CRYPTO_set_id_callback
QSslSocket: cannot resolve CRYPTO_set_locking_callback
QSslSocket: cannot resolve sk_free
QSslSocket: cannot resolve sk_num
QSslSocket: cannot resolve sk_pop_free
QSslSocket: cannot resolve sk_value
QSslSocket: cannot resolve SSL_library_init
QSslSocket: cannot resolve SSL_load_error_strings
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv23_client_method
QSslSocket: cannot resolve SSLv3_server_method
QSslSocket: cannot resolve SSLv23_server_method
QSslSocket: cannot resolve X509_STORE_CTX_get_chain
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
QSslSocket: cannot resolve SSLeay
QSslSocket: cannot call unresolved function CRYPTO_num_locks
QSslSocket: cannot call unresolved function CRYPTO_set_id_callback
QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback
QSslSocket: cannot call unresolved function SSL_library_init
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function sk_num
QSslSocket: cannot call unresolved function SSLv23_client_method3%
QSslSocket: cannot call unresolved function SSL_library_init
Warning: Failed to load https://www.gstatic.com/charts/loader.js (ignore)
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function SSL_library_init
因为我们无法解决这个问题,所以我们决定使用PhantomJS,它可以很好地创建PDF。这里可能是一个输入错误,不是在真实页面上,但值得检查。
onload
属性指向-->initCharts
——而google是在一个名为-->initChart
@WhiteHat的函数中加载的。是的,这是一个打字错误!在initCharts
函数中加载google图表的行之前和之后,我成功地写入了div
。回调函数drawCharts()
从未被调用。您是否检查了浏览器控制台中的错误?您的“加载各种图表的其他函数”与我有关。您是否多次调用google.charts.load?@WhiteHat该页面不会发送到浏览器,而DinkToPdf有一个无头浏览器,可以运行该页面并将其转换为pdf。我找不到从DinkToPdf查看javascript错误的方法。