ASP.net code within include not executing

强颜欢笑 提交于 2019-12-10 17:54:22

问题


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:


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.




回答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.



来源:https://stackoverflow.com/questions/11959125/asp-net-code-within-include-not-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!