web.config 文件中的命名空间不起作用
我是尝试在 web.config 文件中添加命名空间,以便我可以在所有网页中使用它(在代码后面
中)。但它不起作用。
有人试过吗?我之前曾问过这个,但没有得到任何合适的答案。
为了更好地理解这里是代码隐藏
文件
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestWeb
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.BackColor = Color.Red;// It is not working.
}
}
}
这是web.config
文件
<?xml version="1.0"?>
<configuration>
<system.web>
<pages>
<namespaces>
<clear/>
<add namespace="System.Drawing" />
</namespaces>
</pages>
</system.web>
</configuration>
编辑
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
Label1.BackColor = Color.Red;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label ID="Label1" runat="server" text="Label"></asp:label>
</div>
</form>
</body>
</html>
Possible Duplicate:
How to add namespaces in web.config file?
I am trying to add namespace in web.config file, so that I can use it in all web pages (in code behind
). But it is not working.
Has anybody tried it ? I have asked this before but not get any suitable answer.
To better understand here is code behind
file
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestWeb
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.BackColor = Color.Red;// It is not working.
}
}
}
This is web.config
file
<?xml version="1.0"?>
<configuration>
<system.web>
<pages>
<namespaces>
<clear/>
<add namespace="System.Drawing" />
</namespaces>
</pages>
</system.web>
</configuration>
Edit
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
Label1.BackColor = Color.Red;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label ID="Label1" runat="server" text="Label"></asp:label>
</div>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
元素仅适用于 aspx 页面。与 VB.Net 不同,C# 没有在普通代码文件中自动包含命名空间的机制。
The
<namespaces>
element only applies to aspx pages.Unlike VB.Net, C# has no mechanism to automatically include namespaces in ordinary code files.
你应该尝试这个然后再尝试。
You should try this and then try.
命名空间配置部分适用于页面,而不是代码隐藏。文件背后的代码表示其自身内的所有命名空间导入。配置中包含的命名空间将包含在从 .aspx 页面生成的动态类中。
The namespaces configuration section is for pages, not code behind. A code behind file express all namespace imports within itself. The namespaces included in configuration will be included in the dynamic class that is generated from your .aspx page.
正如 SLAks 所说,这是行不通的。如果您正在寻找一种始终包含命名空间的快捷方式,请考虑编辑默认的 VS 模板,以便在创建新模板时它始终存在。
As SLaks says, that will not work. If you're looking for a shortcut to always having a namespace included, consider editing the default VS templates, so that it's always there when you create a new one.