【问题标题】:.NET MVC 5 Only one area not working under IIS.NET MVC 5 只有一个区域在 IIS 下不起作用
【发布时间】:2015-08-13 05:38:21
【问题描述】:

我的项目中有一个名为“报告”的区域存在问题。当我尝试从该区域访问某些控制器时,我总是收到 404 错误。仅当我在本地 IIS (Windows 8.1) 上运行我的应用程序时才会出现此问题。在其他机器(Windows 7 和本地 IIS)上一切正常。即使在这台 Windows 8.1 机器上,但 IIS Express 一切正常。

我试图清除临时文件,但没有结果。

区域注册如下:

public class ReportsAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Reports";
        }
    }

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

而在 Global.asax 中,Application_start() 方法:

AreaRegistration.RegisterAllAreas();

我不知道问题出在哪里。你有什么想法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc iis asp.net-mvc-5 asp.net-mvc-routing


    【解决方案1】:

    检查事件查看器。我遇到了这个问题,“报告”URL 是一个与 SQL Server Reporting Services 相关的虚拟目录。

    【讨论】:

      【解决方案2】:

      试试这个:

      public class ReportsAreaRegistration : AreaRegistration 
      {
          public override string AreaName 
          {
              get 
              {
                  return "Reports";
              }
          }
      
          public override void RegisterArea(AreaRegistrationContext context) 
          {
              context.MapRoute(
                  "Reports_main",
                  "Reports/{controller}/{action}/{id}",
                  new { area = "Reports", controller = "Home", action = "Index", id = UrlParameter.Optional }
              );
              context.MapRoute(
                  "Reports_default",
                  "Reports/{controller}/{action}/{id}",
                  new { action = "Index", id = UrlParameter.Optional }
              );
          }
      }
      

      Reports_default 路由规则将Index 设置为给定控制器的默认操作。

      您需要添加另一个Reports_main 规则,该规则将在您访问该区域的根目录时设置默认控制器,例如/Reports。在此规则中,我假设默认控制器为 Home,但您可以更改它以适合您的项目。

      【讨论】:

        【解决方案3】:

        Visual Studio 的默认AreaRegistration 脚手架不包含controller 的默认值,这意味着需要在 URL 中提供控制器。

        /Reports/Home // This works (if you have a home controller)
        
        /Reports // This doesn't work
        

        要使控制器可选,您需要提供一个默认值。

        public class ReportsAreaRegistration : AreaRegistration 
        {
            public override string AreaName 
            {
                get 
                {
                    return "Reports";
                }
            }
        
            public override void RegisterArea(AreaRegistrationContext context) 
            {
                context.MapRoute(
                    "Reports_default",
                    "Reports/{controller}/{action}/{id}",
                    // Note that controller is defaulted to Home
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }
        

        还要注意,路由注册的顺序与它们将在整个应用程序中执行的顺序相同。通常,这意味着您必须先调用AreaRegistration.RegisterAllAreas(),然后再调用RouteConfig.RegisterRoutes(RouteTable.Routes)

        Tasos 提供的答案也可以,但是配置不正确:

        1. AreaRegistration 中指定默认区域是多余且不必要的。
        2. 在他的示例中,Reports_default 是一个无法访问的执行路径,这使得它也是多余和不必要的。

        【讨论】:

        • 我试过了,但没用。我认为这个问题是其他人。几分钟前,我在 Windows 7 本地 IIS 上运行此应用程序,一切正常。所以我认为问题在于我的计算机上的本地 IIS 配置。
        猜你喜欢
        • 2017-06-03
        • 2017-01-22
        • 2017-02-21
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 2021-08-04
        • 2010-12-15
        • 1970-01-01
        相关资源
        最近更新 更多