【发布时间】:2016-08-16 16:54:20
【问题描述】:
我正在尝试编写要托管的页面,但代码不断抛出异常。我认为问题在于服务器上的权限不会让 XmlUrlResolver 的子类被实例化,但我希望能得到一些帮助......
此代码确实适用于我的本地开发盒,如果我使用 File.ReadAllText(HostingEnvironment.MapPath("~/xml/home.xml")) 它确实有效,所以我相当确定它不是 IO 权限。 ...
例外:
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request failed.
Source Error:
Line 33: t.Load(reader, null, new XsltUrlResolver());
Line 34: sb = new StringBuilder(File.ReadAllText(HostingEnvironment.MapPath("~/xslt/url-encode.xslt")));
Line 35: }
Line 36: </script>
Line 37:
代码(这是我用来尝试缩小问题范围的一个简单页面):
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="Web" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Web.Hosting" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public StringBuilder sb = new StringBuilder();
public class XsltUrlResolver : XmlUrlResolver
{
public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
if (baseUri != null)
return base.ResolveUri(baseUri, relativeUri);
else
return base.ResolveUri(baseUri, HostingEnvironment.MapPath(relativeUri));
}
}
protected void Page_Load(object sender, EventArgs e)
{
// create the readers for the xml and xsl
XmlReader reader = XmlReader.Create(new StringReader(File.ReadAllText(HostingEnvironment.MapPath("~/xslt/home.xslt"))));
XmlReader input = XmlReader.Create(new StringReader(File.ReadAllText(HostingEnvironment.MapPath("~/xml/home.xml"))));
// create the xsl transformer
XslCompiledTransform t = new XslCompiledTransform(true);
t.Load(reader, null, new XsltUrlResolver());
sb = new StringBuilder(File.ReadAllText(HostingEnvironment.MapPath("~/xslt/url-encode.xslt")));
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= sb.ToString() %>
</div>
</form>
</body>
</html>
【问题讨论】: