C# asp.net常见编译|运行错误

来源:互联网 发布:淘宝卖家常遇到的问题 编辑:程序博客网 时间:2024/04/30 14:25

编译器给出的错误描述的朦胧程度是有目共睹的,本文记录了平时常见的编译错误及其正确内容以辅助参考。长期更新。

按第一个有意义的错误的首字母排序,以便查找。


错误: “$”未定义(出现在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">
    <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>

而在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()

错误:private void GetOrDefaultUDC<T>(Table<T> table, string what, int whatID, int mapID, out int id, out Object value)
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。

 

 

点击下载免费的敏捷开发教材:《火星人敏捷开发手册》