MVC 4 Areas not working properly

2019-08-02 14:00发布

I have created a sample VB project in VS2010 to add Areas to the web UI but when I run it gives the following error:

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

I also added AreaRegistration.RegisterAllAreas() to the global.asax file and also tried to manually register the route in the route.config but nothing is working.

routes.MapRoute( _
            "Admin_default", _
            "Admin/{controller}/{action}/{id}", _
            New With {.action = "Index", .id = UrlParameter.Optional} _
            )

It looks like it only finds the root views but not the Area specific view. Any ideas??

2条回答
迷人小祖宗
2楼-- · 2019-08-02 14:34

Make sure you add AreaRegistration.RegisterAllAreas() as first line in Global.asax. Because areas should be registered before registering other routes.

Also in the route you mentioned, there is no default value for controller. So, make sure you provide controller value in url or provide default controller parameter in route.

查看更多
Explosion°爆炸
3楼-- · 2019-08-02 14:38

Found an answer on another site so posting the solution here:

The same project in C# works perfectly fine but fails in VB. The reason: The controllers namespace is wrong in VB.net Solution: Change the namespace of the controller in the vb project to MyApplication.Areas.MyArea.Controllers and then run it, will be fine.

查看更多
登录 后发表回答