开发者

Namespaces in web.config file not working [duplicate]

This question already has answers here: Closed 11 years ago.

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.dt开发者_运维技巧d">
<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>


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.

<configuration>
  <system.web>
    <pages>
      <namespaces>
        <clear/>
        <add namespace="System.Drawing" />
        <add namespace="System" />
      </namespaces>
    </pages>
  </system.web>
</configuration>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜