我正在关注ASP.Net MVC "TaskList" example video并点击在Visual Studio中运行(约14:00进入视频)我在浏览器中收到以下错误消息:
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
我已经仔细检查了我输入的代码,我错过了什么?
谢谢!
版本:
答案 0 :(得分:2)
D'哦!的
发现问题,它位于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
感谢您的建议,我想我下次需要检查我的代码!
(如果编译器指向我的代码而不是Global.asax.vb
,这会让我觉得这是一个配置问题会很好)
答案 1 :(得分:1)
非常有趣。你可以上传完整的源代码或编译的DLL(可能必须从临时的ASP.NET文件夹中获取)吗?我非常怀疑VB编译器在任何情况下都应该生成无效的IL - 所以你可能遇到了编译器中的错误。
答案 2 :(得分:0)
这似乎与第17行的匿名类型有关。确保您的代码看起来像
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With { .controller = "Home", .action = "Index" }
)
如果您需要任何进一步的帮助,请在Application_Start
中发布您的路线答案 3 :(得分:0)
app.UseMvc();
中没有输入或重复的Startup.cs
会导致问题。
真:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
题:
app.UseMvc();