2017-10-12 87 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 –