【发布时间】:2014-02-02 22:33:36
【问题描述】:
今天早上刚开始使用 ASPX 文件,但遇到了障碍。
我收到以下错误:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1519: Invalid token 'try' in class, struct, or interface member declaration
Source Error:
Line 5: int portno = 52791;
Line 6: IPAddress ipa = Dns.GetHostEntry(hostname).AddressList[0];
Line 7: try
Line 8: {
Line 9: System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
Source File: c:\inetpub\wwwroot\test.aspx Line: 7
来自以下 ASPX 文件代码:
<%@ Page Language="C#" %>
<%@ Import namespace="System" %>
<script runat="server">
string hostname = "localhost";
int portno = 52791;
IPAddress ipa = Dns.GetHostEntry(hostname).AddressList[0];
try
{
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
sock.Connect(ipa, portno);
if (sock.Connected == true) // Port is in use and connection is successful
string output = "Port is Closed";
sock.Close();
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10061) // Port is unused and could not establish connection
string output = "Port is Open!";
else
string output = ex.Message;
}
</script>
<html>...
谷歌搜索数小时没有解决我的问题。
【问题讨论】:
-
可能你忘了把这些东西包装在一个方法中?
-
简单的答案是不要在标记中这样做;这是属于后面代码的东西,你的错误会很明显。
-
另外,你在各种范围内重复定义字符串
output,它永远无法使用。这实际上是一个编译器错误,除了荒谬之外。 -
基于this answer,似乎假设您的脚本是Javascript而不是C#,否则您需要明确告知它。
-
对于内联代码,使用
<%和%>而不是<script runat="server">和</script>。