MVC非区域路由问题(沿区域)

发布于 2024-12-06 06:45:47 字数 1690 浏览 6 评论 0 原文

我应该如何配置以下非区域路线?

/foo/{controller}/{action}/{id}
映射到命名空间 myapp.foo 中的控制器。

/{控制器}/{操作}/{id}
映射到命名空间 myapp 中的控制器。

我还有 2 个区域,barbaz,它们是通过 registeraAllAreas 注册的。

我当前的设置

这是我当前的设置。当我使用 url /Home/Index 时,它会出现以下问题。

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("myapp/elmah.axd/{*pathInfo}");

AreaRegistration.RegisterAllAreas();

routes.MapRoute(
    "foo", // Route name
    "foo/{controller}/{action}/{id}", // URL with parameters
    new { action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new string[] { "myapp.Controllers.foo" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new string[] { "myapp.Controllers" }
);

发现多种类型与名为“Menu”的控制器匹配。这 如果服务此请求的路由可能会发生 ('foo/{controller}/{action}/{id}') 未指定命名空间 搜索与请求匹配的控制器。

“菜单”请求已找到以下匹配的控制器: myapp.Controllers.MenuController
myapp.Areas.bar.Controllers.MenuController
myapp.Areas.baz.Controllers.MenuController

显然我做错了一些事情。

更新

我在使用时也生成了错误的地址:

<% using (Ajax.BeginForm("SaveSomething", "Home", ...

它呈现

我猜测人们无法可靠地使用 {controller}同一区域有两条路线。

更新 2

当我将 /foo 路线注册放在底部时,效果似乎更好。
这就提出了一个问题,什么被认为是默认路由? (默认路由建议放在最后面。)

How should I configure the following non area routes?

/foo/{controller}/{action}/{id}
maps to controllers in namespace myapp.foo.

/{controller}/{action}/{id}
maps to controllers in namespace myapp.

I also have 2 areas, bar and baz, they are registered with registeraAllAreas.

My current setup

This is my current setup. It gives the problem below when I use the url /Home/Index.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("myapp/elmah.axd/{*pathInfo}");

AreaRegistration.RegisterAllAreas();

routes.MapRoute(
    "foo", // Route name
    "foo/{controller}/{action}/{id}", // URL with parameters
    new { action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new string[] { "myapp.Controllers.foo" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new string[] { "myapp.Controllers" }
);

Multiple types were found that match the controller named 'Menu'. This
can happen if the route that services this request
('foo/{controller}/{action}/{id}') does not specify namespaces to
search for a controller that matches the request.

The request for 'Menu' has found the following matching controllers:
myapp.Controllers.MenuController
myapp.Areas.bar.Controllers.MenuController
myapp.Areas.baz.Controllers.MenuController

Clearly there's something I'm doing the wrong way.

Update

I also get the wrong adress generated when I use:

<% using (Ajax.BeginForm("SaveSomething", "Home", ...

It renders <form target="/foo/Home/SaveSomething"

I'm guessing that one cannot reliably use {controller} in two routes in the same area.

Update 2

It seems to work much better when I put the /foo route registration at the bottom.
This raises the question, what is considered a/the default route? (As the default route is reccomended to be put at the very end.)

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

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

发布评论

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

评论(1

べ映画 2024-12-13 06:45:47

您有两个名为 MenuController 的控制器,因此如果您不提供更多信息,MVC 就不知道要使用哪一个。在您所在的区域中,您可能有一个名为 AreaRegistration 之类的文件。打开这些文件并更新 RegisterArea 方法,以便将请求路由到正确的控制器。

从您的错误消息来看,路由似乎被映射到 foo/{controller}/{action}/{id},它没有 MenuController。我的猜测是,您在 foo somesomething 下的页面上有一个操作链接。如果您不指定链接区域,将会生成不正确的链接。

尝试通过 ActionLink 使用默认路由:

@Html.ActionLink("Some text", "action", "controller", new { area = "" }, null)

如果您希望请求转到特定区域,只需在调用中写下来即可。

更新: 问题是,当您编写类似 Ajax.BeginForm("SaveSomething", "Home",...) 的内容时,它将匹配第一条路线。您无法按照我之前的建议将区域放入 BeginForm 语句中来解决此问题,因为 foo 路线不是区域。你有两个选择,1:将 foo 部分移动到一个区域,2:将 foo 路由放在默认路由后面。如果将默认路由放在 foo 路由之前,只要 foo 与默认路由位于同一区域(默认区域),就会很难渲染 url,因为路由引擎总是会先找到默认路由。但是,您将能够捕获对 foo 路由的请求。所以我最好的建议是将 foo 路线放在一个区域中。

You have two controllers that has the name MenuController so MVC doesn't know which one to use if you don't give it more information. In you areas you probably have a files named something like <YourAreaName>AreaRegistration. Open those files and update the RegisterArea method so you route the request to the right controller.

From your error message it seems like the route is getting mapped to foo/{controller}/{action}/{id}, which doesn't have a MenuController. My guess is that you have a action link on a page under foo something something. That will generate an incorrect link if you don't specify the area for the link.

Try this to use the default route with ActionLink:

@Html.ActionLink("Some text", "action", "controller", new { area = "" }, null)

If you want the request to go to a specific area just write it down in the call.

UPDATE: The problem is that when you write something like Ajax.BeginForm("SaveSomething", "Home",...) it will match the first route. You can't solve this by putting the area in the BeginForm statement as I suggested before since the foo route is not an area. You have two options, 1: move the foo part to an area, 2: put the foo route after the default route. If you put the default route before the foo route you will get a hard time rendering urls as long as you have foo in the same area as the default route (the default area), since the route engine will always find the default one first. However, you will be able to catch request to the foo route. So my best suggestion is to put the foo route in an area.

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