2008-10-18 72 views
2

我正在關注ASP.Net MVC "TaskList" example video並點擊在Visual Studio中運行(〜14:00分鐘進入視頻)我在瀏覽器中收到以下錯誤消息:ASP.Net MVC錯誤 - 錯誤的IL格式

Server Error in '/' Application. 

Bad IL format. 
Description: An unhandled exception occurred during the execution of the 
current webrequest. Please review the stack trace for more information 
about the error andwhere it originated in the code. 

Exception Details: System.BadImageFormatException: Bad IL format. 

Source Error: 

Line 12:   ' (2) URL with parameters 
Line 13:   ' (3) Parameter defaults 
Line 14:   routes.MapRoute(_ 
Line 15:    "Default", _ 
Line 16:    "{controller}/{action}/{id}", _ 


Source File: C:\Users\...\TaskList\TaskList\Global.asax.vb Line: 14 

Stack Trace: 

[BadImageFormatException: Bad IL format.] 
    VB$AnonymousType_0`3..ctor(T0 controller, T1 action, T2 id) +0 
    TaskList.MvcApplication.RegisterRoutes(RouteCollection routes) in 
    C:\Users\...\TaskList\TaskList\Global.asax.vb:14 
    TaskList.MvcApplication.Application_Start() in 
    C:\Users\...\TaskList\TaskList\Global.asax.vb:23 


Version Information: 
    Microsoft .NET Framework Version:2.0.50727.1434; 
    ASP.NET Version:2.0.50727.1434 

我已經仔細檢查了輸入的代碼,我錯過了什麼?

謝謝!

版本:

  • ASP.Net MVC Beta版(2008年10月16日)
  • 視覺Studion 2008(9.0.21022.8 RTM)
  • Vista旗艦版SP1
  • IIS 7.0.6000.16386

回答

2

D'oh!

發現了問題,它在HomeController.vb

Public Class HomeController 
    Inherits System.Web.Mvc.Controller 

    ' Display a list of tasks 
    Function Index() 
     Return View() 
    End Function 

    ' Dislpay a form for creating a new task 
    Function Create() As ActionResult 
     Return View() 
    End Function 

    ' Adding a new task to the database 
    Function CreateNew(ByVal task As String) As ActionResult 
     ' add the new task to the database 
     Return RedirectToAction("Index") 
    End Function 

    ' Mark a task as complete 
    Function Complete() 
     ' database logic 
     Return RedirectToAction("Index") 
    End Function 

End Class 

Function Complete()缺少返回類型,應改爲:

' Mark a task as complete 
    Function Complete() As ActionResult 
     ' database logic 
     Return RedirectToAction("Index") 
    End Function 

感謝您的建議,我想我需要tripple-check下次我的代碼!

(雖然這將是很好,如果編譯器指着我的代碼,而不是Global.asax.vb,這讓我覺得這是一個配置問題)

0

它似乎與第17行上的匿名類型有關。請確保您的代碼看起來像

routes.MapRoute(_ 
    "Default", _ 
    "{controller}/{action}/{id}", _ 
    New With { .controller = "Home", .action = "Index" } 
) 

如果您需要任何進一步的幫助,請張貼在的Application_Start

1

非常有趣的路線。你可以上傳完整的源代碼或編譯的DLL(可能需要將它從臨時的ASP.NET文件夾中取出)?我非常懷疑VB編譯器在任何情況下都應該生成無效的IL - 所以你可能在編譯器中遇到了一個錯誤。

+0

會做 - 但代碼是在家裏,所以我會今晚發佈它:O) – Andrew 2008-10-20 07:52:51

+0

我正要張貼代碼,當我發現這個問題 - 看到我的答案:http://stackoverflow.com/questions/215719/aspnet-mvc-error-bad-il-format#219769 – Andrew 2008-10-21 07:52:51

0

一個app.UseMvc();沒有投入或Startup.cs複製一個可能會導致問題。

真:

 app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 

假:

 app.UseMvc();