Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

c# - MVC 3 Area route not working

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

In global.asax I have the following code.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

    }

This is the code of my Area

public class BlogAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Blog"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Blog_default",
            "Blog/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

When I go to the following url http://localhost/CMS/blog I get the following error.

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/blog/Index.aspx ~/Views/blog/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/blog/Index.cshtml ~/Views/blog/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml

How do I solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...