MVC 3 Area route not working

前端 未结 8 731
有刺的猬
有刺的猬 2020-12-17 18:54

I created a Area in my MVC 3 application called \'Blog\'.

In global.asax I have the following code.

public static void RegisterRoutes(RouteCollection         


        
相关标签:
8条回答
  • 2020-12-17 19:10

    Check if you have a virtual path is mapped as the area name. I set area address in visual studio just to debug and it asked me to create a virtual path. So AppRelativeCurrentExecutionFilePath was always ~/ and routing wasn't able to determine the area. For IIS express delete the virtual path for your site:

    C:\Users\username\Documents\IISExpress\config\applicationhost.config
    

    I spent 4 days to discover it.

    0 讨论(0)
  • 2020-12-17 19:12

    I found what I consider to be a bug in the framework, with a workaround. If you are trying to map a default route to an MVC 3 app with areas, your global.asax file might have something like this:

    VB:

    routes.MapRoute(
          "Default",
          "{area}/{controller}/{action}/{id}",
          New With {.area = "MyArea", .controller = "Home", .action = "Index", .id = UrlParameter.Optional}
    )
    

    C#:

    routes.MapRoute(
           "Default",
           "{area}/{controller}/{action}/{id}",
           new { area = "MyArea", controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    If you go to your app root in the URL, you may get a runtime error like this:

    The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

    For some reason, the view engine does not appear to look in the area folder for the view file the same as if you type in the whole link. The strange thing is the code reaches the controller action. Here is the fix: Put this code in your controller action:

    VB:

    If Not Me.ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
                    Me.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
                End If
    

    C#

      if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))
    {
            this.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
     }
    
    0 讨论(0)
  • 2020-12-17 19:12

    Relax! This can save you hours of reading, make a coffee and watch this 3 minutes video, everything will be clear to you. http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas (I belive it also works for mvc3, mvc4, and mvc2035) enter image description here

    0 讨论(0)
  • 2020-12-17 19:17

    I'm using Phil Haack's routedebugger to troubleshoot problems such as this one. It conveniently shows all registered routes and how the entered URL matches your configuration.

    It works by overriding the regular application flow, which you enable by adding this line at the end of Application_Start:

    RouteDebug.RouteDebugger.RewriteRoutesForTesting( RouteTable.Routes );
    
    0 讨论(0)
  • 2020-12-17 19:18

    Following on from Mortens answer, you can now NuGet (or manually download and install) the RouteMagic, or Glimpse packages which provides this functionality, and more.

    More information is available on Phil Haacks blog regarding the state of his tool and what it has morphed into. The comments make for good reading too!

    0 讨论(0)
  • 2020-12-17 19:19

    Just try to delete content from following directories as mentioned here and rebuild project

    C:\Temp C:\Users\%Username%\AppData\Local\Microsoft\VisualStudio C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files Path\To\Your\Project\obj\Debug

    0 讨论(0)
提交回复
热议问题