2012-12-18 33 views
0

我有一個MVC項目,我有一個全局函數返回url的。這裏是其中之一:訪問資源文件中的全局變量

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Account.Models 
{ 
    public class ApplicationStrings 
    { 

     public static string ApplicationString(){ 

      return "http://localhost11234/Studentbook"; 

    } 

    } 

} 

我有資源文件,我存儲電子郵件正文爲不同的電子郵件出去。這是一個嵌入式資源。

如何從我的資源文件中的全局函數中訪問URL?

我嘗試以下方法,但沒有看到鏈接:

<html> 
<body> 
<br> Student ID : <a href= ApplicationStrings.ApplicationString() + ?id=JD1123>JD1123</a><br>  
</body> 
</html> 

我看到以下內容:

Student ID : JD1123 

這裏JD1123是一個鏈接,該鏈接帶我到「ApplicationStrings.ApplicationString ()「而不是」http:// localhost11234/Studentbook?id = JD1123「

回答

0

您不能做你想做的事,至少不能直接做。資源字符串是靜態項目,當您訪問它們時不會自動更改。所以基本上,你正準備退出。計算機不夠聰明,不知道應該調用一個方法來讓你的配置字符串停留在那裏。

相反,您需要編寫一段中間代碼來搜索您的資源字符串以替換特定的項目(可能將它們標記爲<%或其他),然後使用Regex.Replace插入正確的文本。這是相當微不足道的。

http://www.codeguru.com/cpp/cpp/string/regex/article.php/c2791/Using-Regular-Expressions-for-SearchReplace.htm