.NET MVC 5 只有一个区域不能在 IIS 下工作
.NET MVC 5 Only one area not working under IIS
我的项目中有一个区域 'Reports' 有问题。当我试图从这个区域访问某个控制器时,我总是得到 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();
我不知道问题出在哪里。你有什么想法吗?
试试这个:
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
,但您可以更改它以适合您的项目。
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 }
);
}
}
另请注意,路由的注册顺序与它们在整个应用程序中的执行顺序相同。通常,这意味着您必须在调用 RouteConfig.RegisterRoutes(RouteTable.Routes)
.
之前调用 AreaRegistration.RegisterAllAreas()
Tasos 提供的答案也可以,但配置不正确:
- 在
AreaRegistration
中指定默认区域是多余且不必要的。
Reports_default
在他的例子中是一个无法到达的执行路径,这使得它也变得多余和不必要。
检查事件查看器。我遇到了这个问题,'Reports' URL 是一个与 SQL Server Reporting Services 有关的虚拟目录。
我的项目中有一个区域 'Reports' 有问题。当我试图从这个区域访问某个控制器时,我总是得到 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();
我不知道问题出在哪里。你有什么想法吗?
试试这个:
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
,但您可以更改它以适合您的项目。
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 }
);
}
}
另请注意,路由的注册顺序与它们在整个应用程序中的执行顺序相同。通常,这意味着您必须在调用 RouteConfig.RegisterRoutes(RouteTable.Routes)
.
AreaRegistration.RegisterAllAreas()
Tasos 提供的答案也可以,但配置不正确:
- 在
AreaRegistration
中指定默认区域是多余且不必要的。 Reports_default
在他的例子中是一个无法到达的执行路径,这使得它也变得多余和不必要。
检查事件查看器。我遇到了这个问题,'Reports' URL 是一个与 SQL Server Reporting Services 有关的虚拟目录。