gpt4 book ai didi

asp.net-mvc - ASP.Net MVC 4. 在单独的程序集中路由 Controller 返回错误 404

转载 作者:行者123 更新时间:2023-12-03 17:52:30 25 4
gpt4 key购买 nike

我正在尝试执行看起来很简单的任务 - 从我的项目中的单独程序集中调用 Controller 。

但是,当我向该 Controller 请求信息时,我收到了错误 404。过去 5 到 6 个小时我一直在与它作斗争,我怀疑我可能遗漏了一些小而明显的东西,这就是我希望得到建议的原因。

我在 stakoverflow 上发现了有关路由和错误 404 的类似问题,但是当我实现另一个原型(prototype)项目时,我没有收到此类错误并使用类似的代码结构,我相信他们描述的问题与我的不同。

对于一般情况 - 我想要实现的总体目标是在一个独立项目中实现一个区域,就像它所描述的 here .
我做了所有事情,就像链接中解释的那样,一个小原型(prototype)(它工作得很好),现在正试图申请真正的项目。

这就是我包含装配和管理路线的方式:

  • 有一个主项目,其中一个区域代表隐藏的子项目“CommunicationBus”
  • “CommunicationBus”项目包含一个“CommunicationBusAreaRegistration”类。注册路由正常工作,我可以使用调试器到达这里,当我使用 routedebugger 时我也可以看到这条路由(见下面的截图)。我也玩过这个类的命名空间,尝试添加 .Areas.CommunicationBus最后,但我没有注意到任何不同。
    namespace MBVD.MainProject.UI
    {
    public class CommunicationBusAreaRegistration : AreaRegistration
    {
    public override string AreaName
    {
    get
    {
    return "CommunicationBus";
    }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
    context.MapRoute(
    "CommunicationBus_default",
    "CommunicationBus/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new string[] { "CommunicationBus.Controllers" }
    );
    }
    }
    }
  • 在主项目的 Global.asax 中,我通过以下方式注册路由:
    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "MBVD.MainProject.UI.Controllers" }
    );
    }

    如果我在这里引用“CommunicationBus.Controllers”,我将无法再打开主应用程序的页面。但是,我相信我只需要在此处注册主应用程序 Controller 的路径
  • CommunicationBus 项目的输出路径设置为主项目的 bin 文件夹。每次我构建 CommunicationBus 时,我都会在主项目中获得一个新的 .dll。
  • 我在 CommunicationBus 项目中添加了一个简单的 Controller :
    namespace CommunicationBus.Controllers
    {
    public class TestController : Controller
    {
    [HttpGet]
    public string Index()
    {
    return "It works!";
    }
    }
    }
  • 我添加了一个指向主项目的链接
     @Html.ActionLink("test","Index","Test", new {Area="CommunicationBus"}, null)

    这是:

    本地主机:63254/CommunicationBus/测试
  • 我使用 routedebugger,它没有显示任何可疑的东西:

  • routedebugger

    我对这两个项目都使用了 ASP.NET MVC 4。

    我将不胜感激有关我可以做些什么来弄清楚为什么会出现此错误的任何想法。

    最佳答案

    我相信由于您的路由顺序,您会收到此错误。由于/CommunicationBus/Test匹配/{controller}/{action}/{id}/CommunicationBus/{controller}/{action}/{id}第一个注册的路由 ( /{controller}/{action}/{id} ) 优先于所需的路由,并且 MVC 在名为 MBVD.MainProject.UI.Controllers 的默认命名空间 ( CommunicationBus ) 中找不到 Controller .

    尝试在默认路线之前注册您的区域路线。或者,您可以一起跳过该业务并使用属性路由 (http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx)。如果您使用属性路由,您需要确保在调用 routes.MapMvcAttributeRoutes(); 之前从所有命名空间加载 Controller 。 (如果您只是作为引用,这没问题,只有在动态加载引用时才会成为问题)。

    关于asp.net-mvc - ASP.Net MVC 4. 在单独的程序集中路由 Controller 返回错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17471918/

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