区域路线不起作用

区域路线不起作用

本文介绍了MVC 3 区域路线不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 MVC 3 应用程序中创建了一个名为博客"的区域.

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

在 global.asax 中,我有以下代码.

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);

    }

这是我所在地区的代码

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

当我转到以下网址 http://localhost/CMS/blog 时,出现以下错误.

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

未找到视图索引"或其主视图,或者没有视图引擎支持搜索的位置.搜索了以下位置:〜/视图/博客/Index.aspx〜/视图/博客/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?

推荐答案

您所在地区的注册似乎有误.您为操作指定默认值,但不为控制器指定默认值.由于您通常将 Home 作为控制器的名称,因此您需要指定它.

The registration in your area appears to be wrong. You specify a default for your action but not for the controller. Since you typically have Home as the name of the controller you'd need to specify that.

也可能是您没有正确设置文件夹,因为您应该进行物理设置:

Also it could be you don't have your folders setup correctly since you should have physically setup:

  • /地区/博客
  • /区域/博客/控制器
  • /区域/博客/视图

...一旦您修复了您的博客区域路线,您还需要:

... and once you have fixed your blog area route you'll also need:

  • /Areas/Blog/Views/Home
  • 将您的索引视图放在此处

你得到的错误似乎很清楚地表明这是问题所在.

The error you get seems to pretty clearly indicate this is the issue.

这篇关于MVC 3 区域路线不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:39