gpt4 book ai didi

asp.net - MVC 路由与现有文件夹冲突导致 404

转载 作者:行者123 更新时间:2023-12-02 04:07:51 25 4
gpt4 key购买 nike

我确实看到了类似问题的答案,但没有一个适用于这种情况。

我有 MVC4 应用程序,其注册区域称为“设置”,带有一个“管理” Controller 。我的 Web 项目中还有名为“Settings”的源文件夹,因此文件夹结构如下:

+ WebProjectFolder
+ Areas
+Settings
+Controllers
ManageController.cs
+ SettingsAreaRegistration.cs
...
+ Settings
+ SomeClasses.cs

ManageController 非常简单:

public class ManageController : Controller
{
public ActionResult Index()
{
return Content("WORKS FINE!");
}
}

区域注册也很简单:

public class SettingsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Settings";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoute("Settings/{*pathinfo}");
context.Routes.RouteExistingFiles = false;

context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new {area = context.AreaName, controller="Manage", action = "Index", id = UrlParameter.Optional }
);
}
}

问题:

http://mysite/settings - 404 
http://mysite/settings/Manage - 404
http://mysite/settings/Manage/Index - 404

如果我删除行“context.Routes.IgnoreRoute("Settings/{*pathinfo}");”结果如下:

http://mysite/settings - 404
http://mysite/settings/Manage - WORKS FINE!
http://mysite/settings/Manage/Index - WORKS FINE!

仅当我删除/重命名现有文件夹“设置”时,结果才是正确的。与将 Controller 命名为与文件夹相同的情况类似。

如何让 MVC 忽略现有文件夹并始终优先考虑 Controller?

最佳答案

我想你想设置 RouteExistingFilestrue

context.Routes.RouteExistingFiles = true;

摘录:

true if ASP.NET routing handles all requests, even those that match an existing file; otherwise, false. The default value is false.

关于asp.net - MVC 路由与现有文件夹冲突导致 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18970807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com