views:

292

answers:

4

I have an ASP.NET application which features some server-side includes. For example:

<!--#include virtual="/scripts.inc" -->

These files are not present in my ASP.NET website project because my website starts in a virtual directory:

/path-to-my-application

When I choose Build Web Site, I get this error:

Failed to map the path '/scripts.inc'

Visual Studio cannot resolve these include files that are defined at the root directory level. They are not visible in the website project.

Aside from manually commenting out the #include references, is there any way I can get the website to build? Can I force Visual Studio to ignore those errors and compile the site?

Once the website is pushed out to IIS, there is no problem, because all the #include files are in place.

NOTE - Web Controls are not an option for this application. Please assume #include files are a requirement. Also, I cannot move the include files since they are used by other applications.

A: 

Try using the ~/ syntax to represent the root of your app. e.g.

<!--#include virtual="~/scripts.inc" -->
David Ebbo
This won't work - this points to the path /path-to-my-application/scripts.inc, which does not exist. The inc file is at /scripts.inc.
frankadelic
I can I misunderstood you. I thought you said it wasn't finding it at /scripts.inc because it was actually at the root of the app. If the file isn't there, there is no way of telling the parser not to fail.
David Ebbo
That's weird ... the compiler/parser thinks /scripts.inc is in the application root. IIS thinks /scripts.inc is at the web root.
frankadelic
Have you set up your virtual directory as an application in IIS?
Kaerber
@Kaerber: yes... IIS is behaving OK. The problem is in VS, which doesn't have a concept of Virtual Directories.
frankadelic
A: 

Hi frankadelic, try this:

Replace the SSI directive in your .aspx file with this:

<asp:Literal runat="server" id="scriptsIncLiteral" />

And put this in your code-behind:

protected override void OnPreRender(EventArgs e)
{
    string scriptsFile = Request.MapPath(".") + @"..\scripts.inc";
    scriptsIncLiteral.Text = System.IO.File.OpenText(scriptsFile).ReadToEnd();
}

You will of course have to change the number of ..\s if the scripts.inc file is located more than one directory up. You will also have to ensure that your ASP.NET web application has access to this file, otherwise you'll get a System.UnauthorizedAccessException.

Warren
It's a hack, quite fragile.A better way to implement this hack would be<% Response.WriteFile( Server.MapPath( "/Scripts.inc" ) ) %>More info herehttp://support.microsoft.com/kb/306575
Kaerber
A: 

You can set up a pre-build task in Visual Studio to copy include file in the project directory each time the project is built before the actual build. Although this is a hack.

A more correct way would be to set up a solution with two projects: one will represent your web-server's root directory/a set of the applications with which your project interacts (through including a shared file). Another will represent your troubled project. Then you should include (as in include as files into the project) your inc file(s) into the first project, for it to make them visible for a second project and allow it to include (as in server-side include in ASPX) them.

It is a way with more hassle, but it mirrors your situation much more closely, no hack, and can bring you some bonus features farther along the road (like easier intergration/representation of connected projects).

Kaerber
A: 

Can you make a copy of the includes files, place them in your solution on your dev machine and then tell VS not to copy those on build output (Build Action = None)?

If not, why don't you just hard code the entire link to the scripts.inc file (http://oursite.com/scripts.inc). Sucky work around, but I am pretty sure that you can't just ignore compilation errors (but yes to warnings).

Tommy