ASP.NET MVC 中如何选择生成传出 URL 的路由?

发布于 2024-10-25 21:48:59 字数 376 浏览 4 评论 0原文

再会!

似乎正在发生一些代码魔法

我正在使用 ASP.NET MVC 2 和 T4MVC,当我将其添加到路由表时,

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

:当我在视图中编写类似的内容以生成传出时,框架如何知道我希望应用此规则URL:

<%: Url.Action(MVC.Profile.Login() %>

如果我对同一控制器/操作对有多个不同的规则(具有不同的参数)怎么办?会选择哪一位呢?有没有关于这种行为的很好的描述?

提前致谢!

Good day!

I'm using ASP.NET MVC 2 and T4MVC and it seems some code magic is happening

When I add this to routes table:

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

How does framework know that I want this rule to apply when I write something like this in the view to generate outgoing URL:

<%: Url.Action(MVC.Profile.Login() %>

What if I have multiple different rules (with different params) for the same controller/action pair? Which one will be chosen? Is there anywhere a good description of this behavior?

Thanks in advance!

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

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

发布评论

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

评论(3

爱的十字路口 2024-11-01 21:48:59

我建议帮助您理解这项工作是如何将 T4MVC 的魔力与 MVC 本身在幕后所做的事情区分开来。

当您使用 T4MVC 编写此内容时:

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

相当于使用直接 MVC 编写此内容:

routes.MapRoute(
    "Login",
    "login/",
    new { controller = "Profile", action = "Login" }
);

并且在视图中:

Url.Action(MVC.Profile.Login())

与 T4MVC相同

Url.Action("Login", "Profile")

,为您提供强类型/智能感知的好处,但最终它的作用与直接 MVC 相同。

希望这有助于澄清一些事情:)

What I would suggest to help you understand how this work is to separate the magic that T4MVC does from what MVC itself does under the cover.

When you write this with T4MVC:

routes.MapRoute(
    "Login",
    "login/",
    MVC.Profile.Login()
);

It's equivalent to writing this with straight MVC:

routes.MapRoute(
    "Login",
    "login/",
    new { controller = "Profile", action = "Login" }
);

And in the view:

Url.Action(MVC.Profile.Login())

Is the same as

Url.Action("Login", "Profile")

T4MVC gives you the benefit of strong typing/intellisense, but in the end what it does is the same as with straight MVC.

Hopefully this helps clear things up a bit :)

倾城花音 2024-11-01 21:48:59

它按照您定义的顺序匹配路由模式。这就是为什么你将默认模式作为最后一个模式。一旦找到匹配的模式,它就会停止寻找。

编辑

参数在路由匹配过程中被忽略。选择控制器方法后,mvc 使用模型绑定将参数分配给方法变量。

如果您可以解释您想要使用什么类型的 url 结构,我们可能可以为您提供更多帮助。

It matches the route patterns in the order you define them. Thats why you have the default pattern as the last one. As soon as it finds a matching pattern, it stops looking.

Edit

Parameters are ignored during route matching. Once a controller method has been selected, mvc uses model binding to assign the parameters to the method variables.

If you can explain what type of url structure you are looking to use, we can probably help you more.

梦在深巷 2024-11-01 21:48:59

您的示例不是有效的 MVC,您通常会传递控制器名称、操作和任何其他参数,然后路由引擎将使用所有这些信息来确定要使用哪个路由,您定义的路由越多,它可能会提供的信息就越多需要确定您想要匹配的那个

Your example is not valid MVC, you would normally pass the controller name, action and any other parameters, then the routing engine would use all that information to determine which route to use, the more routes you have defined, the more information it will probably require to determine the one YOU want to match

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