代码在根目录中运行,但不在子目录中运行

时间:2011-01-11 22:26:17

标签: asp.net

我有一些奇怪的麻烦。

我有一段代码

 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"     Inherits="_Default" %>

    <!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 runat="server">
        <title>VERSIONS CHECK!!!</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="lblVersion" runat="server" />
        </div>
        <br />
        <div>
            <asp:Label ID="lblHelloWorld" runat="server" />
        </div>
        <p>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
       </p>
        </form>
    </body>
    </html>

和代码隐藏文件:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
{
    lblVersion.Text = System.Environment.Version.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (lblHelloWorld.Text.Equals(string.Empty))
    {
        lblHelloWorld.Text = "Hello World";
    }
    else
    {
        lblHelloWorld.Text = "";
    }
}
}

这适用于本地和我的public_html文件夹。但是,如果我在子文件夹中添加相同内容,例如public_html/folder1/folder2,则会出现错误。

Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error     settings for this application prevent the details of the application error  from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

我是ASP.NET新手并使用visual studio发布 - 所以很有可能我在某处做错了。实际上,这是我第一次尝试编写ASP.NET - 所以要温和:)

提前谢谢。

杰克这就是我的配置文件现在的样子:

Visual Studio中的Asp.Net配置选项。     可以在中找到完整的设置和注释列表     machine.config.comments通常位于     \的Windows \ Microsoft.Net \框架\其中Vx.x \配置 - &GT;               

<system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
    <authentication mode="Windows"/>


<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
</configuration>

我现在得到的错误是:

'/'应用程序中的服务器错误。 配置错误 描述:处理为此请求提供服务所需的配置文件时发生错误。请查看下面的具体错误详细信息并适当修改配置文件。

分析程序错误消息:在应用程序级别之外使用注册为allowDefinition ='MachineToApplication'的部分是错误的。此错误可能是由于虚拟目录未在IIS中配置为应用程序。

来源错误:

Line 26:             ASP.NET to identify an incoming user. 
Line 27:         -->
Line 28:        <authentication mode="Windows"/>
Line 29: 
Line 30: 

2 个答案:

答案 0 :(得分:2)

要弄清楚您收到的错误,您需要在web.config文件中看到customErrors为“Off”。您可以将其设置为RemoteOnly但我不认为您在实际的服务器框上查看它,因此将其设置为关闭。

<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">

</customErrors>

这将显示您收到的错误。

答案 1 :(得分:2)

子目录中是否有web.config个文件?如果是这样,您在该子目录web.config中设置authentication mode吗?

您无法从子目录设置身份验证模式:ASP.NET Configuration File Hierarchy and Inheritance