2017-10-12 86 views
0

嗨,我想创建使用空网站的asp.net应用程序。 我已经创建了一个App_Code文件夹的一个文件夹的Utils内创建在其内部一个“CommonConstants.cs”文件,宣告一些属性和要如下所示来访问文件的Login.aspx那些性质,但它显示“Common.Constants确实不包含“APPLICATION_TITLE”,同时也内CommonConstants.cs显示错误的属性定义“的类型或命名空间名称APPLICATION_TITLE“找不到”。任何想法将不胜感激。类不包含定义其属性

CommonConstants.cs

public class CommonConstants 
{ 
    public const APPLICATION_TITLE = "Description of Project"; 
    public const FOOTER_YEAR = "2016"; 
    public const APPLICATION_DESC = "Short Desc"; 
    public const LOGIN_TITLE = "Login"; 

} 

的Login.aspx

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

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />Utils 
    <link rel="Shortcut Icon" href="Images/favicon.ico" /> 
    <title><%CommonConstants.APPLICATION_TITLE%></title> 
+0

你的财产d小甜甜是错误的。您需要为每个属性指定类型。 '公共常量字符串APPLICATION_TITLE =“项目的说明”;'' –

+0

=的CodeFile“Login.aspx.cs”'那么,这是什么'CommonConstants'定义它? –

+0

@RubenVardanyan感谢它的工作。 – micky

回答

0

由于该类没有标记为静态你需要先创建该类的实例,然后调用它的性能一样

CommonConstants commonconstant = new CommonConstants(); 

然后调用它的属性这样的

commonconstant.APPLICATION_TITLE; 

,你也能做到这一步在后面的代码访问类

protected CommonConstants _commonconstant; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    _commonconstant = new CommonConstants(); 
} 

然后在ASPX

_commonconstant.APPLICATION_TITLE 

阅读静态here

+0

不需要将类标记为静态。您可以访问'const'属性而不创建新的实例。 –

+0

@RubenVardanyan我试过,但不能直接访问类的不变的性质。你能不能请示 – 2017-10-16 07:41:25

+0

https://gist.github.com/rubvardanyan/68f82df1d607931fc57cb7408dd5a91a –

相关问题