MVC 中的路径中的花括号意味着什么?

发布于 2024-12-13 17:33:30 字数 194 浏览 4 评论 0原文

我正在使用 MVC 项目设置我的路线,但我对大括号有点困惑...

如果我有...

routes.MapRoute( "Music", "Music/{name}", new {  } );

名称周围的大括号的目的是什么,这是否会传递给某些东西?或者如果我传入默认对象,这会映射到某些东西吗?

I'm setting up my routes with an MVC project but im a little confused about the curly braces...

If I have...

routes.MapRoute( "Music", "Music/{name}", new {  } );

What is the purpose of the curly braces around name, does this get passed to something? Or does this map to something if I pass a default object in?

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

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

发布评论

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

评论(3

苏璃陌 2024-12-20 17:33:30

它们是在路由请求中使用的参数名称。例如,默认路由定义了其中三个:

{controller}/{action}/{id}

controlleraction 参数用于查找控制器操作。 id 参数可以用作这些操作中的输入。

当您定义自定义路由时,您必须提供controlleraction 参数。如果您的 URL 中没有定义它们,您应该提供默认值,以便 MVC 知道当请求与该路由匹配时要运行什么操作。

  routes.MapRoute("Music",
                  "Music/{name}",
                   new { controller="Music", action="SomeAction" });

您定义的其他参数(例如 idname)可用于为操作提供输入。在您的示例中, name 参数被传递给匹配的操作,如下所示:

public ActionResult SomeAction(string name)
{
    //do something
}

They are parameter names that are used in routing requests. For example the default route defines three of them:

{controller}/{action}/{id}

controller and action parameters are for finding your controller action. id parameter can be used as an input in those actions.

When you define a custom route you have to provide controller and action parameters. If they are not defined in your URL, you should provide default values so MVC knows what action to run when a request matches that route.

  routes.MapRoute("Music",
                  "Music/{name}",
                   new { controller="Music", action="SomeAction" });

Other parameters like id or name like you defined can be used to provide input to actions. In your example, name parameter is passed to matching action like this:

public ActionResult SomeAction(string name)
{
    //do something
}
っ左 2024-12-20 17:33:30

大括号表示一种命名通配符。

"Music/Index" 路由将仅匹配 URL Music/Index 而不会匹配任何其他内容

"Music/{Name}" 路由将匹配任何以 Music 开头且斜线后包含任何内容的 URL。它将匹配 URL Music/metalicaMusic/madonna

使用大括号,您将能够从上面的 URL 中选择“metalica”或“madonna”作为路由值。

最后一个例子:对于 ASP.NET MVC,总是有一个标准路线。 {控制器}/{动作}/{id}。此路由将捕获诸如 Music/genre/rockProduct/edit/5 之类的 URL。
这两个的最终路由值将是:

  • 第一个路由的controller=music、action=genre 和id=rock,最后一个路由的
  • controller=product、action=edit 和id=5。

The curlybraces indicate a kind of named wildcard.

The "Music/Index" route will only match the URL Music/Index and nothing else

The "Music/{Name}" route will match any URLs starting with Music, and having anything after the slash. It will match both the URLs Music/metallica and Music/madonna.

With the curly brace, you'll be able to pick up "metallica" or "madonna" from the above URLS as routevalues.

As a final example: With ASP.NET MVC, there's always a standard route. {controller}/{action}/{id}. This route will catch URLs like Music/genre/rock or Product/edit/5.
The resulting routevalues for these two will be:

  • controller=music, action=genre and id=rock for the first one
  • controller=product, action=edit and id=5 for the last one.
偏爱你一生 2024-12-20 17:33:30

我将尝试提供一个不那么做作的例子。

ASP.NET MVC 中的路由被放入字典中,当有传入请求时,MVC 管道会查看该请求并尝试确定将其路由到哪个控制器和操作。

假设我有以下控制器:HomeForumArticle

当我们这样做时,假设我有以下 控制器操作:论坛文章 控制器上的查看编辑创建

这些大括号允许我为两者创建一条路线:

routes.MapRoute("Viewing",
    {controller}/{action}/{id},
    new {controller = "Article", action="" },  //The article controller has precedence
    new { controller = "Article|Forum" } //contrived for this example
);

这些大括号意味着它们放入的任何控制器(只要它是基于约束的 ArticleForum),都是相同的路线有效。这使我不必为论坛和文章控制器中的每个操作都制定一条路线。

我本来可以轻松地制作两条路线:

routes.MapRoute("Articles",
    article/{action}/{id},
    new {controller = "Article" } //The article controller has precedence
);

routes.MapRoute("Forums",
    forum/{action}/{id},
    new { controller = "forum" }
);

但是那里有不需要的重复。

路线也是相当棘手的事情,顺序很重要。顶部路线将在底部路线之前评估。如果它与顶部路由的结构匹配,它将转到该操作,即使这不是正确的操作。

Phil Haack 有一个路由调试器可以帮助解决这个问题。我还获取了他的源代码并对其进行了修改,以便您可以 使其成为一个控件,并将其作为部分内容放在所有页面上(希望您也将代码放在那里,只允许内部人员看到它)。

I'll try to provide a less contrived example.

Routes in ASP.NET MVC are placed into a dictionary, and when there's an incoming request, the MVC pipeline looks at the request and tries to determine what Controller and Action to route it to.

So let's say I have the following controllers: Home, Forum, and Article

And while we're at it, let's say I have the following actions: View, Edit, Create on both the Forum and Article controllers.

Those braces allow me to create one route for both:

routes.MapRoute("Viewing",
    {controller}/{action}/{id},
    new {controller = "Article", action="" },  //The article controller has precedence
    new { controller = "Article|Forum" } //contrived for this example
);

Those braces mean that whatever controller they put in (as long as it's Article or Forum based on the Constraints), the same route works. This keeps me from having to have a route for each and every action in the Forum and Article controller.

I could have just as easily made two routes:

routes.MapRoute("Articles",
    article/{action}/{id},
    new {controller = "Article" } //The article controller has precedence
);

routes.MapRoute("Forums",
    forum/{action}/{id},
    new { controller = "forum" }
);

But there's duplication there that doesn't need to be there.

Routes are also pretty tricky things, in that order matters. The top route will be evaluated before the bottom route. If it matches the top route's structure, it will go to that action, even if that's not the right action.

Phil Haack has a Route Debugger that helps with this. And I've also taken his source code and modified it so that you can make it a control and put it on all your pages as a partial (and hopefully you will also put code on there that would only allow internal folks to see it).

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