I've spent a good part of the last 3 days trying to figure out a way around this. I'm hoping someone can point out what I'm doing wrong or confirm that this is a bug in .NET.
I have a WCF service that renders UserControls via the Server.Execute method. It then parses out the script tags, css tags, and generated HTML and returns a JSON formatted object with an array of script tags, an array of css tags, and the HTML as the response.
This worked fine in .NET 3.5 SP1. However, in .NET 4 I'm finding that any virtual path referenced in the User Control that I'm rendering doesn't take into account the fact that the application is located in a virtual directory (this problem doesn't exist when the application isn't hosted in a virtual directory).
In essence virtual paths like "~/Images/image.png" or script references like "~/Script/script.js" used to render as "Images/image.png" and "Script/script.js" are now resolving to "../Images/image.png" and "../Script/script.js". So now I have a lot of 404's from the invalid URLs.
[OperationContract]
public string LoadControl(string control)
{
StringWriter myTextWriter = new StringWriter();
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
HttpContext.Current.Server.Execute("~/LoadControl.aspx?&id=" + control , myWriter, true);
...
}
Here is the code behind for LoadControl.apsx:
protected void Page_Load(object sender, EventArgs e)
{
//Get the base ID to use for the generated control
string baseId = Request["baseId"];
ControlIDGenerator.SetIDPrefix(baseId);
string control = "~/" + Request["id"];
Page.Controls.Add(Page.LoadControl(control));
}
A couple of things I've noticed:
1.The virtual directory resolution is related to the path of the .SVC file that is used as an endpoint for the service. If I move the .SVC file into a deeper folder path another "../" is added to the resolved path.
2. Any script resource for WebControls from assemblies resolve correctly. A script reference to a file resolves to "/VirtualDirectory/ScriptResource.axd?...."
Any thoughts would be greatly appreciated.