如果您从 ISV\yoursolution\web.config 中删除 CrmAuthentication 和 MapOrg 模块,那么您将无法在调用 Web 服务时重新使用最终用户的身份。
此解决方案将允许您在 CRM 4.0 中使用 UpdatePanel 和 ScriptManager,而无需修改 CRM 的 web.config,也无需将您的 ISV 解决方案文件夹设为自己的 IIS 应用程序。
为此,我们需要使用响应过滤器修复 ScriptManager 控件的输出,以便 Web 浏览器尝试请求 ISV 解决方案文件夹中的 ScriptResource.axd 文件。然后,为了说服 Script Resource Handler 处理请求,它必须看起来像是在根目录中被请求的,原因有两个。因此,我们需要创建并连接我们自己的脚本资源处理程序,它将简单地修复请求并将请求代理到普通处理程序。
要拦截和修复脚本标签,请在您的 aspx 页面的 Load 事件中:
protected void Page_Load(object sender, EventArgs e)
{
Response.Filter = new ScriptResourceFixupFilter(Response.Filter);
}
然后连接处理程序以拦截请求,修改您的 ISV\yoursolution\web.config 文件。在 configuration\system.web\httpHandlers 部分,注释掉指定 path="ScriptManager" 的 add 元素并插入一个新的 add 元素,如下所示:
<!--<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>-->
<add verb="GET,HEAD" path="ScriptResource.axd" type="YourNamespace.ScriptResourceHandlerProxy, YourAssemblyNameWithoutDllExtension"/>
确保设置类型属性,以便它引用项目中类的命名空间和程序集名称。
然后在您的解决方案中包含这两个类:
/// <summary>
/// This is used to resolve compatibility issues with CRM's web.config, it doesn't have entries required for
/// the built in System.Web.Handlers.ScriptResourceHandler used Microsoft's ASP.Net Ajax components (i.e. ScriptManager, UpdatePanel, etc...)
///
/// This class will pick up the request for the script resource,
/// translates the request url so it appears to come at the to the app root path
/// </summary>
public class ScriptResourceHandlerProxy : System.Web.Handlers.ScriptResourceHandler
{
protected override void ProcessRequest(HttpContext context)
{
// in order to trick the ScriptResourceHandler into handling this request,
// we need to show it that the path of the request context is at the root of the web application
var uri = new UriBuilder(context.Request.Url.AbsoluteUri)
{
Path = VirtualPathUtility.ToAbsolute("~/ScriptResource.axd"),
Query = null
};
var compatableContext = new HttpContext(
new HttpRequest("ScriptResource.axd", uri.Uri.ToString(), (context.Request.Url.Query ?? String.Empty).TrimStart('?')),
context.Response);
base.ProcessRequest(compatableContext);
}
}
/// <summary>
/// This is used to resolve compatibility issues with CRM's web.config, it doesn't have entries required for
/// the built in System.Web.Handlers.ScriptResourceHandler used Microsoft's ASP.Net Ajax components (i.e. ScriptManager, UpdatePanel, etc...)
///
/// Replace references to src="/ScriptResource.axd" with "/ISV/YourSolution/ScriptResource.axd" so that the
/// ASP.Net runtime picks up on our web.config entries that include the asp.net Ajax entries and passes the
/// request along to our script resource handler proxy class
/// </summary>
public class ScriptResourceFixupFilter : MemoryStream
{
private Stream _output;
public ScriptResourceFixupFilter(Stream output)
{
this._output = output;
}
public override void Write(byte[] buffer, int offset, int count)
{
var content = UTF8Encoding.UTF8.GetString(buffer);
content = content.Replace(@"""/ScriptResource.axd", @"""/ISV/YourSolution/ScriptResource.axd");
_output.Write(UTF8Encoding.UTF8.GetBytes(content), offset, UTF8Encoding.UTF8.GetByteCount(content));
}
}
祝你好运!