Recently, I encountered a very strange problem at work. It was very strange to find the cause before I found it undefined. I would like to share with you this article to remind you of similar situations.
Error background
I wrote about 10-20 lines of code in the View to dynamically generate the WebGrid column. According to the customer's opinion, I want to move the code to an independent c # code. So I added a Class library and moved all the code. Write the called code and run the code. An error is reported. This error has never been seen before. I carefully read the code and thought it was okay, but I don't understand why it went wrong. I changed the code a little and ran another strange error. After reading the code, I still feel that there is no problem, but I still don't understand why there is a problem. The following is the details.
Error 1: Entry point was not found.
This is the main code. This section of c # code has all been moved to this c # class. After running, View throws an Exception: Entry point was not found, and points to @ grid. GetHtml.
using System;using System.Collections.Generic;using System.Web.Mvc;using System.Web.Helpers;namespace ClassLibrary1{ public static class myGrid { public static List<WebGridColumn> GenerateGridColumn(WebViewPage page) { var list = new List<WebGridColumn>(); var gridColumn = new WebGridColumn(); gridColumn.ColumnName = "test"; gridColumn.Header = " test header"; list.Add(gridColumn); var gridColumn1 = new WebGridColumn(); gridColumn.Format = item => page.Html.Raw("<a href=\"test\"></a>"); list.Add(gridColumn1); return list; } }}
MyGrid. cs
@model List<Class1>@using ClassLibrary1;@{ ViewBag.Title = "Index";}
View code
After reading the code many times and debugging it, I can't see the problem. I noticed a strange phenomenon in the debugging process, myGrid. generateGridColumn should return a List <WebGridColumn>. When trying to view its value in the Watch window, an unknown error occurs: Unable to evaluate the expression. operation not supported. unknown error: 0x8004f0ed. this brings about a lot of problems. Debugging cannot be seen. Where is the problem. Although there is no obvious evidence, I can see that there is an Html in this code, and I suspect the problem caused by Html. Raw. I think this Html. Raw may have to be in the View to work properly. So I tried to remove the code containing Html. Raw from the c # class code and put it in the View. The code is like this:
using System;using System.Collections.Generic;using System.Web.Helpers;namespace ClassLibrary1{ public static class myGrid { public static List<WebGridColumn> GenerateGridColumn() { var list = new List<WebGridColumn>(); var gridColumn = new WebGridColumn(); gridColumn.ColumnName = "test"; gridColumn.Header = " test header"; list.Add(gridColumn); return list; } }}
MyGrid. cs modified
@model List<Class1>@using ClassLibrary1;@{ ViewBag.Title = "Index";}
View code modified
In this case, some items in the List are added to myGrid, and the other part is added to View. However, the error message is as follows:
Error 2: Attempted to access an element as a type incompatible with the array
After reading this error message, I also checked the code. After debugging for a long time, I always thought that the code was okay, but I just didn't understand why the problem occurred. During debugging, the List <WebGridColumn> type is still the same and cannot be viewed in the Watch window. the error message is still the same: Unable to evaluate the expression. operation not supported. unknown error: 0x8004f0ed, compared with MyGrid. the cs code and View code clearly add the WebGridColumn type element to the List. They should be the same element and cannot report this error. You cannot figure it out.
As a result, some people mentioned on the Internet that it may be Web. the System. web. helpers. dll. config, Web. config is correct. Then, continue to find the problem. After looking for two days, I still cannot find the root cause of the problem. Later, I checked the References of each project and suddenly found that the ClassLibrary actually References System. Web. Helpers. dll of version 1.0. This may be the problem. So I changed 1.0 to 2.0 Reference, and all these errors disappeared. I was delighted.
Experience
Asp.net mvc has many dll files with the same name but different versions. We can upgrade them from the old version to the new version, or construct a new version of asp.net mvc application on the Web. in config, make sure that the correct version of dll is referenced in the reference of each project. Otherwise, we will not know which trap we fall.
Error Code download: http://dl.vmall.com/c0wdxad9am for those who read the text or do not understand prepared.
If you think this article is worthy of recommendation, please help with the recommendation.