Ane*_*ook 5 asp.net-mvc routing
我正在尝试执行看似简单的任务 - 从我的项目中的单独程序集调用控制器.
然而,当我从该控制器请求信息时,我得到错误404.我一直在与它斗争最后5-6小时,我怀疑我可能会遗漏一些小而明显的东西,这就是为什么我希望得到一个建议.
我在stakoverflow上找到了关于路由和错误404的类似问题,但是当我实现另一个原型项目时,我没有得到这样的错误并使用类似的代码结构,我相信他们描述的问题与我的不同.
总的来说 - 我想要实现的目标的总体目标是在独立项目中实现一个区域,就像这里描述的那样.我做了所有类似的事情,在链接中解释了一个小型原型(并且它工作正常),现在尝试申请真正的项目.
这是我包含程序集和管理路由的方式:
"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" }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)在主项目的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" }
);
}
Run Code Online (Sandbox Code Playgroud)
如果我在这里引用"CommunicationBus.Controllers",我就不能再打开主应用程序的页面了.但是,我相信我只需要在这里注册主应用程序的控制器的路径
CommunicationBus项目的输出路径设置为主项目的bin文件夹.每次我构建CommunicationBus时,我都会在主项目中获得一个新的.dll.
我在CommunicationBus项目中添加了一个简单的控制器:
namespace CommunicationBus.Controllers
{
public class TestController : Controller
{
[HttpGet]
public string Index()
{
return "It works!";
}
}
}
Run Code Online (Sandbox Code Playgroud)我添加了一个主项目的链接
@Html.ActionLink("test","Index","Test", new {Area="CommunicationBus"}, null)
Run Code Online (Sandbox Code Playgroud)
这是:
本地主机:63254/CommunicationBus /测试
我使用routedebugger,它没有显示任何可疑的东西:

我将ASP.NET MVC 4用于这两个项目.
我会很感激我能做些什么来弄清楚为什么我会收到这个错误.
我相信您收到此错误是因为您的路由顺序。由于/CommunicationBus/Test两者都匹配/{controller}/{action}/{id},并且/CommunicationBus/{controller}/{action}/{id}第一个注册的路由 ( /{controller}/{action}/{id}) 优先于所需的路由,并且 MVC 无法在名为 的默认命名空间 ( MBVD.MainProject.UI.Controllers)中找到控制器CommunicationBus。
尝试在默认路线之前注册您的区域路线。或者,您可以完全跳过该业务并使用属性路由(http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5 .aspx)。如果您使用属性路由,则需要确保在调用之前从所有命名空间加载控制器routes.MapMvcAttributeRoutes();(如果您只是作为引用包含在内,这没有问题,只有在动态加载引用时才会成为问题)。
| 归档时间: |
|
| 查看次数: |
2141 次 |
| 最近记录: |