天天看點

C# asp.net常見編譯|運作錯誤

編譯器給出的錯誤描述的朦胧程度是有目共睹的,本文記錄了平時常見的編譯錯誤及其正确内容以輔助參考。長期更新。

按第一個有意義的錯誤的首字母排序,以便查找。

錯誤: “$”未定義(出現在master/layout的jquery中)2011-05-01

原因1:若在MVC中,一種原因是連結參數問題。比如應該調用controller/action?peopleID=34,但卻直接調用了controller/action/34(一般會被解析為controller/action?id=34而不是peopleID),結果出錯。把調用連接配接換成controller/action?peopleID=34就可能能解決。

另外一種情況是參數仍為id,但卻無法識别controller/action/34而隻能識别controller/action?id=34,原因可能是global.acax.cs裡邊的route代碼出了問題,筆者尚未弄清楚,但隻要保持controller/action?id=34就可以正常運作。

Cannot convert from 'method group' to 'string'

錯誤:return repSFC.GetAllViewFitlers(vc.A, vc.C, vc.T).OrderBy(i => i.Order);

正确:return repSFC.GetAllViewFitlers(vc.A(), vc.C(), vc.T()).OrderBy(i => i.Order);

說明:其實是錯把函數當成屬性了。

cshtml字尾不識别(警告) 2011-05-07

There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'. C:/Users/...

說明:這是因為原來的vs2010不認識csttml,不知道如何對其進行編譯。

解決:在web.config裡邊兩個地方加上:

<assemblies>

...

<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</assemblies>

<providers>

<add extension=".cshtml" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>

</providers>

Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'? 2011-05-04

錯誤:還是把函數當成屬性了,比如:@foreach(var sprint in Sprint.Sprints)

正确:@foreach(var sprint in Sprint.Sprints()),或者幹脆聲明Sprints為屬性。

JQuery未定義(運作某些頁面時彈出)2011-05-03

錯誤:layout或master中包含了錯誤的Jquery版本,就是這一帶出了問題:

<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>

<script src="../../Scripts/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>

正确:請檢查此頁面所用的主機闆與其他能正常運作的頁面所有主機闆中對script的引用的差别,拷貝正确的來就可以了。

說明:ViewBag中的所有變量都是Object,是以如果ViewFilterMapsOf被重載過,這裡不知道是調用的具體哪個(盡管人眼能看出來,但機器不能)。

RenderPartial錯誤: 'System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<Martian.Areas.SFC.Models.SFCViewFilter>>' has no applicable method named 'RenderPartial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

錯誤:@{Html.RenderPartial("_SubMenuContentFilters", @ViewBag.Route);}

正确:@{Html.RenderPartial("_SubMenuContentFilters", @ViewBag.Route as MVCRoute);}

說明:RenderPartial不能動态解析(dynamically dispatched)ViewBag中的随時生成的類型,需要顯式轉換(casting)。不過這點微軟設計不好,因為Partial中有model聲明,資訊足以進行解析。

'object' does not contain a definition for 'ToList'

錯誤:List<SFCViewFilterMap> filters = SFCViewFilterMap.ViewFilterMapsOf(Model, ViewBag.FilterGroup).ToList();

正确:List<SFCViewFilterMap> filters = SFCViewFilterMap.ViewFilterMapsOf(Model, ViewBag.FilterGroup as string).ToList();

說明:還是老問題,ViewBag的變量不會自動轉換為實際類型。

Error: Object doesn't support property or method 'XXX'

錯誤:重複引用了script (如jquery)

場景1:

<head runat="server">

……

@{Html.RenderPartial("_Scripts");}

</head>

<body runat = "server">

而在Html.RenderPartial("_Scripts")中,已經引用了下面兩個script。

The model of type XXX could not be updated.

錯誤:UpdateModel(item, "story.Title");

正确:UpdateModel(item, "story");

說明:好像造成這種錯誤的原因很多,Google上有各種答案。而這次錯誤最終發現是不小心把最終要更新的字段名Title也放到prefix裡邊了。

The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table<TEntity>' 2011-05-25

錯誤:private void GetOrDefaultUDC<T>(Table<T> table, string what, int whatID, int mapID, out int id, out Object value)

where T: IUDC, new()

where T: class, IUDC, new()

說明:關鍵詞是reference,見到它就補一個class(盡管從IUDC派生多半已經是class了)。這裡出現此限制的原因是在Table的聲明中,聲明了凡是Table<T>中的T必須是個class,具體聲明是:public sealed class Table<TEntity> : IQueryProvider, ITable, IListSource, ITable<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable where TEntity : class

System.Data.Linq.ChangeConflictException: Row not found or changed.

現象:如果在編輯一個控件後,UpdateModel後Save時出這個錯誤,特别是如果編輯兩個以上字段的時候出現1 of n updates failed,則基本确定是LINQ出問題了。

如果使用LINQ,又在資料庫表中設定了外鍵(Relationship裡邊),然後拖拽到DBML中看到了箭頭,則這個錯誤就難以避免。

這個錯誤是個著名的LINQ Bug,從09年就有,至今未進行修改。唯一處理辦法,是删除所有Relationship,然後就好了。

很多人是以而選擇EF而非LINQ。

本文轉自火星人陳勇 51CTO部落格,原文連結:http://blog.51cto.com/cheny/1100082