3

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.

1
  • Note that Server Side includes are definitely possible, albeit rather ungainly, in asp.net. I've clarified my answer. Commented Aug 14, 2012 at 20:23

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Surprised this chainsaw hammering wasn't accepted. The include route is how you used to play OO in Classic ASP-land [and now when I'm mocking up a page for MVC out of MVC -- don't tell anyone ;)]. Thanks.
0

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.

3 Comments

I have heard User Controls are the better way to go. However, given a time crunch, my ability to learn a new paradigm is hindered... I was hoping I was just doing something more generally wrong.
@sdowswell - WebForms are a very leaky abstraction. Confused me quite a lot when I moved from classic ASP. They were created in order to help VB6 developers feel like they were programming a stateful application with events etc... It may be easier for you if you have been using WinForms for a while.
I've never been one for interim steps... I guess I have some homework to do. Thanks for your guidance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.