2014-05-08 41 views
0

我在上線63編譯錯誤:編譯錯誤

<div class="dateAdded">Article submitted @article.DateAdded.ToRelativeDateStringUtc()</div> 

編譯器錯誤信息:CS1061:「System.DateTime的」不包含「一 定義ToRelativeDateStringUtc」和沒有擴展方法 ‘ToRelativeDateStringUtc’接受 類型的第一參數‘的System.DateTime’可以找到(是否缺少using指令或 程序集引用?)

這是我的錯誤發生的地方。我有一個包含我的程序中包含的ToRelativeDateStringUtc()的類。這是一個MS類,我必須導入?我沒有看到它在.net參考列表中的任何地方列出。我確定它很簡單,有沒有人有任何想法?

回答

0

您應該將所需的名稱空間添加到您的web.config中。根據您是否使用剃鬚刀,有不同的部分。

對於剃刀

<system.web.webPages.razor> 
    <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
    <namespaces> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     . 
     . 
     <!-- etc --> 
    </namespaces> 
    </pages> 
</system.web.webPages.razor> 

對於舊版本的ASP.NET

<pages> 
    <namespaces> 
    <add namespace="System.Web.Mvc"/> 
    <add namespace="System.Web.Mvc.Ajax"/> 
    <add namespace="System.Web.Mvc.Html"/> 
    <add namespace="System.Web.Routing"/> 
    <add namespace="System.Web.WebPages"/> 
    <add namespace="System.Web.Helpers"/> 
    <add namespace="MyCustomHelpers"/> 
    </namespaces> 
</pages> 

最後,您可以添加和@using添加.cshtml的頂部。

+0

我覺得你就在這裏。我添加了命名空間我的配置。指着我的幫手類。現在我看看這一行:第68行:編譯器錯誤消息:CS1001:標識符預期 – Killdashnine

+0

現在好了,那個錯誤神奇地消失了。我以前的錯誤仍然是 – Killdashnine

+0

@Deadhat:你應該添加類似於<'。您添加了對文件的引用,而不是名稱空間。 – Pieter

0

您沒有將參數傳遞給ToRelativeDateStringUtc方法。

this.should樣子:

public static string ToRelativeDateStringUtc(this DateTime date) 
{ 
    return GetRelativeDateValue(date, DateTime.UtcNow); 
} 
private static string GetRelativeDateValue(DateTime date, DateTime comparedTo) 
{ 
    TimeSpan diff = comparedTo.Subtract(date); 
    if (diff.TotalDays >= 365) 
     return string.Concat("on ", date.ToString("MMMM d, yyyy")); 
    if (diff.TotalDays >= 7) 
     return string.Concat("on ", date.ToString("MMMM d")); 
    else if (diff.TotalDays > 1) 
     return string.Format("{0:N0} days ago", diff.TotalDays); 
    else if (diff.TotalDays == 1) 
     return "yesterday"; 
    else if (diff.TotalHours >= 2) 
     return string.Format("{0:N0} hours ago", diff.TotalHours); 
    else if (diff.TotalMinutes >= 60) 
     return "more than an hour ago"; 
    else if (diff.TotalMinutes >= 5) 
     return string.Format("{0:N0} minutes ago", diff.TotalMinutes); 
    if (diff.TotalMinutes >= 1) 
     return "a few minutes ago"; 
    else 
     return "less than a minute ago"; 
} 

更多細節

http://dotnetslackers.com/articles/aspnet/5-Helpful-DateTime-Extension-Methods.aspx

http://forums.asp.net/t/1880820.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

+0

-1這不是擴展方法通常的工作方式。這段代碼應該被稱爲'myDateTimeInstance.ToRelativeDateStringUtc()',編譯器將解析參數。 – sisve

+0

@SimonSvensson我覺得你很聰明,你能解決我的問題嗎? http://stackoverflow.com/questions/23386532/web-security-in-ie-vs-chrome-firefox-bug –