ASP.NET MVC 是否为区域创建默认路由

发布于 2024-10-15 09:22:37 字数 583 浏览 1 评论 0原文

我的 MVC 3 应用程序“身份验证”和“用户”中有几个区域。我正在使用 Phil Haacks 路由调试工具来查看我的路线列表,并查看根据我的网址选择哪一条路线。

然而,有几条路线我没有在我的 AreaRegistration 文件或 Globalasax 中创建,我不知道它们来自哪里,也不知道如何摆脱它们。这些路线在下面以黄色突出显示。

您还可以看到我在我的身份验证区域中创建了一个默认路由(以绿色突出显示),它仅指向我的身份验证控制器的登录操作。我已经调试了 RouteTable,并且在 AreaRegistration.RegisterAllAreas(); 时添加了它。方法被调用。但是,它不会添加到 AreaRegistration 中,因为也已完成此操作。

ASP.NET MVC 是否将其添加为默认值,如果是,我可以以某种方式删除它吗?

在此处输入图像描述

I have a couple of areas in my MVC 3 application Auth and Users. I am using Phil Haacks Route Debugging tool to view a list of my routes and see which one gets selected based on my url.

However there are a couple of routes present that I have not created in either my AreaRegistration file or Globalasax and I don’t know where they have come from or how to get rid of them. The routes are highlighted in yellow below.

You can also see that I have created a default route in my Auth area (highlighted in green) which simply points to the Login action of my Auth controller. I have debugged the RouteTable and it gets added when the AreaRegistration.RegisterAllAreas(); method is called. However it does not get added in the AreaRegistration as have stepped through this also.

Does ASP.NET MVC add this as a default and if so can I remove it somehow?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2024-10-22 09:22:37

我不想回答我自己的问题,但经过一天的尝试解决这个问题后,我想我会发布答案,以防其他人遇到同样的问题。

最后,我从应用程序中删除了所有区域,只拥有基本的 Global.asax 路由。当我运行该应用程序时,我可以在路由调试器中看到路由集合仍在填充来自现在不存在区域的路由。在尝试了很多事情之后,包括删除 ASP.NET 临时文件中的所有内容、摆弄 IIS AppPools 以及清理浏览器数据,我终于找到了答案。

我删除了网站 bin 文件夹中的所有内容,进行了重建,然后发现路线消失了。我使用所描述的配置恢复了我的区域,一切都正常工作。

我不知道为什么我的 MVC 应用程序保留并填充旧路由,但一旦我的垃圾箱被清除并且新的 dll 创建后,一切都会按预期工作。如果有人知道为什么会这样,那么我会非常感兴趣。

I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.

In the end I got rid of all my areas from my application and just had the basic Global.asax routing. When I ran the app I could see in the route debugger that the routing collection was still being populated with the routes from now non existing areas. After trying many things including deleting everything from my ASP.NET temp files, messing around with IIS AppPools and cleaning out browser data I finally came across the answer.

I deleted everything from the websites bin folder, did a rebuild and low and behold, the routes were gone. I reinstated my areas with the config described and everything is working as it should.

I have no idea why my MVC app was holding onto and populating the old routes but as soon as my bin was cleared and new dll’s created everything worked as it should. If anybody out there knows why this may be then I would be very interested.

树深时见影 2024-10-22 09:22:37

是的,每个区域都有自己的区域注册文件,用于定义区域路线。在您所在区域的根文件夹中查找它。

对于您的用户区域,请查看区域 ->用户-> UserAreaRegistration.cs

它应该包含如下内容:

    public class UserAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "User";
            }
        }

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

Yes, each area has it's own AreaRegistration file that defines area routes. Look for it in your area root folder.

For your User area, look in Areas -> User -> UserAreaRegistration.cs

It should contain something like this:

    public class UserAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "User";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "User_default",
                "User/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
在风中等你 2024-10-22 09:22:37

你重命名你的项目了吗?它通过反射加载路由,可能是通过扫描 bin 文件夹中的所有内容。因此,如果您重构代码并更改程序集名称,您可以轻松地选择旧代码并注册这些路由。

柔佛州

Did you rename your project? It loads the routes by reflection, probably by scanning everything in the bin folder. So if you refactored your code and changed the assembly name, you could have easily had old code being picked up and registering those routes.

JB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文