Question

It's been a long time since I dabbled server-side, but it seems to me that scripts embedded in an included code file should execute as normal. This doesn't seem to be the case for some reason.

(Note-- The below is obviously a simplified implementation based on my attempts at debugging. I've actually got other includes with flat HTML and JavaScript in the actual project that render just fine. It's just ASP code that is not being parsed properly, <% %> tags and all.)

INDEX CODE

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Site</title>
</head>

<body>

    <% Response.WriteFile ("includes/test.aspx") %>

</body>
</html>

INCLUDED CODE

<% response.write("boo"); %>

The resulting page, when run from a server, includes the file just fine... but the script is rendered as text.

Where have I gone wrong here??

Thanks so much for your help.

Was it helpful?

Solution

Nothing is going wrong.

When you WriteFile, the contents of the file is going to be rendered.

ASP.NET doesn't have a facility for sever side includes the way classic ASP did.

You need to use controls to build up a page dynamically, though you may want to look at ASP.NET/MVC instead of WebForms, as it is closer to how you would have done things with classic ASP.

OTHER TIPS

I think you may still be thinking in an asp-classic mindset.

Asp.net WebForms attempts to use a more object-oriented approach which use classes, separation of concerns with code behind and inherit look and feel using master pages and place holders, rather than doing includes. Also, WebForms is largely being superceded by ASP.NET MVC, which changes the paradigm again.

However asp-classic style Server Side includes still work just fine in .aspx, with a few restrictions such as the inability to include up through a parent path, and you will also lose your intellisense in the included files.

To use SSI, use the <!--#include file="xxx.ext" --> directive.

So in your example:

<body>
    <!--#include file="includes/test.aspx" -->
</body>

Where test.aspx is simply:

<% int someInt = 123;
Response.Write(someInt);
%>

But IMO is a bit like using a chainsaw to hammer nails. I would skip WebForms entirely and head straight into Asp.Net MVC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top