获取 XslCompiledTransform.Load 以加载 SharePoint 列表中的文件
我在使用 XslCompiledTransform.Load
方法获取服务器路径时遇到困难。我用谷歌搜索了一下,发现我需要做类似的事情:
xslt.Load(System.Web.HttpContext.Server.MapPath(xslPath),
XsltSettings.Default, new XmlUrlResolver());
但它返回了一个错误,说 HttpContext
is null。
我也尝试过:
xslt.Load(System.Web.HttpServerUtility.MapPath(xslPath),
XsltSettings.Default, new XmlUrlResolver());
这也返回了一个错误,指出非静态字段、方法或属性需要对象引用 System.Web.HttpServerUtility.MapPath(string)
xslPath 有一个指向的路径到 Sharepoint Web 中的 xsl 文件。我只想 XslCompiledTransform 加载带有服务器路径的 xsl 文件。是否可以?如果是这样,正确的方法或黑客的方法是什么?
编辑:我可以访问包含 xsl 文件路径的 SPWeb 对象。但是,当我检查 ServerRelativeUrl 时,它只是显示“/MyTree/xsl.xsl”。这里的问题是我无法让 XslCompiledTransform.Load 从 SharePoint 列表加载文件。
谢谢。
I'm having difficulties getting XslCompiledTransform.Load
method to take a server path. I googled around and found that I need to do something like:
xslt.Load(System.Web.HttpContext.Server.MapPath(xslPath),
XsltSettings.Default, new XmlUrlResolver());
But it returned an error saying HttpContext
is null.
I also tried:
xslt.Load(System.Web.HttpServerUtility.MapPath(xslPath),
XsltSettings.Default, new XmlUrlResolver());
That also returned an error saying an object reference is required for the non-static field, method, or property System.Web.HttpServerUtility.MapPath(string)
The xslPath has a path that points to a xsl file in Sharepoint Web. I just want XslCompiledTransform to load the xsl file with the server path. Is it possible? If so, what is the proper way or hackish way of doing it?
EDIT: I have access to a SPWeb object which contains the path to the xsl file. However when I check the ServerRelativeUrl, it just says "/MyTree/xsl.xsl". The problem here is I couldn't get the XslCompiledTransform.Load to load the file from SharePoint list.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在处理请求期间,当前的 HttpContext 是 HttpContext.Current。在页面/用户控件/WebPart 中,这也是属性
Context
。如果在处理请求期间未调用您的方法,则 HttpContext.Current 将为 null。在这种情况下,您可以手动映射路径。
以上是一般情况下在 ASP.NET 中映射磁盘路径的情况。
如果该文件包含在您的 SPWeb 对象中,您应该使用 SPWeb.GetFile
During a the processing of a request, the current HttpContext is HttpContext.Current. In a Page/UserControl/WebPart this is also the property
Context
.If your method is not called during the processing of a request, HttpContext.Current will be null. In this case you could map the path manually.
The above is for mapping disk paths in ASP.NET in general.
If the file is contained in you SPWeb object, you should use SPWeb.GetFile
我最终发现,如果我传入一个与本地路径不同的 url,XslCompiledTransform 类将自动切换到不同的模式以将路径读取为 URL。
如果我传入一个仅包含文件名的字符串,XslCompiledTransform 将在我的本地硬盘中查找该文件。同样,如果我传递类似 /myFolder/myXsl.xsl 的内容。
但是,如果我传入一个共享点 URL,例如 web.ParentWeb.Url + NameOfFile,那么它将从共享点 URL 中读取它。
我不是 100% 确定为什么它会自动切换,但至少上述对我有用。
What I found in the end is that if I pass in a url that does not resemble a local path, the XslCompiledTransform class will automatically switch to a different mode to read the path as a URL.
If I pass in a string that contains only the name of the file, XslCompiledTransform will look for the file in my local hard disk. Likewise if I pass in something like /myFolder/myXsl.xsl.
However, if I pass in a sharepoint URL e.g. web.ParentWeb.Url + NameOfFile, then it'll go off to read it from the Sharepoint URL.
I'm not 100% sure why it does the automatic switch, but at least the above worked for me.