gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 路由 - 所有看起来都匹配正确的路由模板,但不起作用

转载 作者:行者123 更新时间:2023-12-04 21:46:52 24 4
gpt4 key购买 nike

我有一个带有“一般”路由结构的站点和一个附加的“翻译”区域。所以在顶层,你有类似的东西:

  • /
  • /关于(“通用”)
  • /翻译(地区)

  • 在翻译区域内,我有:
  • /Translate/Word(WordController,索引操作)
  • /Translate/Word/Add(WordController,添加操作)
  • /Translate/Word/Improve(WordController,改进 Action )

  • 所有这些工作。

    但是,只要我在现有结构中添加另一个 Controller ,例如。定义 ...
  • /Translate/Definition(定义 Controller ,索引操作)
  • /Translate/Definition/Add (DefinitionController, Add action)

  • 或发音...
  • /Translate/Pronunciation (PronunciationController, Index action)
  • /Translate/Pronunciation/Add (PronunciationController, Add action)

  • 我得到的只是一个 404。

    我区的路由配置如下:
    public override void RegisterArea(AreaRegistrationContext context)
    {
    context.MapRoute(
    "Translate_direct",
    "Translate/{controller}/{action}",
    new { action = "Index", controller = "Default", }
    );
    context.MapRoute(
    "Translate_default",
    "Translate/{toLanguageCode}/{query}/{fromLanguageCode}/{controller}/{section}/{action}/{id}",
    new { action = "Index", controller="Default", id = UrlParameter.Optional, section=UrlParameter.Optional }
    );
    }

    我已安装 Phil Haack's RouteDebugger并得到以下结果:

    Screenshot

    所以它似乎匹配 Controller 和 Action ,但我只得到一个 404。

    为了完整性和保证,还显示了我的“MVC”文件结构:

    enter image description here

    我显然错过了一些明显的东西,但不确定是什么......?

    更新:

    似乎我的命名空间以某种方式参与其中。

    我的 TranslateAreaRegistration 类的命名空间为 Taggloo.Web.Client.Areas.Translate 但我的 PronunciationController 的命名空间为 *Taggloo.Web.**Mvc.**Client.Areas.Translate.Controllers*

    似乎我的命名空间中的错误导致两者不同(TranslateAreRegistration 类也应该在 Taggloo.Web.Mvc.Client.Areas.Translate 命名空间中)是问题所在。我试着让它们都是 Taggloo.Web.Mvc ......但这没有用。我不情愿地将两者都设置为 Taggloo.Web.Client... 而现在它可以工作了。

    所以不是一个解决方案,一个解决方法,尽管环顾四周,我仍然不知道为什么。

    最佳答案

    如果您使用 AreaRegistrationContext.MapRoute 重载之一,MVC 中的默认区域注册 ( System.Web.Mcv.AreaRegistration.RegisterAllAreas ) 使用区域的 AreaRegistration 派生类的命名空间将命名空间数据标记添加到路由定义。

    如果定义了此 Namespaces 标记,则 DefaultControllerFactory 然后在匹配路由时交叉检查 Controller 的命名空间。

    如果 Controller 的命名空间相同(或者如果命名空间标记以 .* 结尾)标记,那么工厂将匹配 Controller 类型并继续实例化 Controller 。如果不匹配并且 ControllerFactory 从 GetControllerType 方法返回 null,最终您将得到 404。

    您提到您将两者都设置为 Taggloo.Web.Mvc .... 如果它们相同,它应该可以工作。

    关于asp.net-mvc - ASP.NET MVC 路由 - 所有看起来都匹配正确的路由模板,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13110552/

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