2

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.

frankadelic
  • 20,543
  • 37
  • 111
  • 164
  • in your solution explorer window, are your includes recognized as part of your project? if not, you can click the icon at the top of your solution explorer window that "Shows All Files" ... right click and "include in project". – TheGeekYouNeed May 26 '10 at 01:18
  • The include files are not in the Visual Studio project. My VS project just contains files that will be deployed in the virtual directory /path-to-my-application. As mentioned above, the includes are at higher level. They would be present in the deployed website, but not inside the virtual directory. – frankadelic May 26 '10 at 19:04

4 Answers4

0

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

<!--#include virtual="~/scripts.inc" -->
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • 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 Jun 03 '10 at 17:37
  • 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 Jun 04 '10 at 04:46
  • That's weird ... the compiler/parser thinks /scripts.inc is in the application root. IIS thinks /scripts.inc is at the web root. – frankadelic Jun 04 '10 at 19:33
  • Have you set up your virtual directory as an application in IIS? – Kaerber Jun 10 '10 at 08:25
  • @Kaerber: yes... IIS is behaving OK. The problem is in VS, which doesn't have a concept of Virtual Directories. – frankadelic Jun 10 '10 at 17:16
0

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 Rumak
  • 3,824
  • 22
  • 30
  • 1
    It's a hack, quite fragile. A better way to implement this hack would be <% Response.WriteFile( Server.MapPath( "/Scripts.inc" ) ) %> More info here http://support.microsoft.com/kb/306575 – Kaerber Jun 10 '10 at 08:40
0

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
  • 1,623
  • 16
  • 21
0

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
  • 39,592
  • 10
  • 90
  • 121