MVC 中的路径中的花括号意味着什么?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们是在路由请求中使用的参数名称。例如,默认路由定义了其中三个:
controller
和action
参数用于查找控制器操作。id
参数可以用作这些操作中的输入。当您定义自定义路由时,您必须提供
controller
和action
参数。如果您的 URL 中没有定义它们,您应该提供默认值,以便 MVC 知道当请求与该路由匹配时要运行什么操作。您定义的其他参数(例如
id
或name
)可用于为操作提供输入。在您的示例中,name
参数被传递给匹配的操作,如下所示:They are parameter names that are used in routing requests. For example the default route defines three of them:
controller
andaction
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
andaction
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.Other parameters like
id
orname
like you defined can be used to provide input to actions. In your example,name
parameter is passed to matching action like this:大括号表示一种命名通配符。
"Music/Index"
路由将仅匹配 URLMusic/Index
而不会匹配任何其他内容"Music/{Name}"
路由将匹配任何以 Music 开头且斜线后包含任何内容的 URL。它将匹配 URLMusic/metalica
和Music/madonna
。使用大括号,您将能够从上面的 URL 中选择“metalica”或“madonna”作为路由值。
最后一个例子:对于 ASP.NET MVC,总是有一个标准路线。
{控制器}/{动作}/{id}
。此路由将捕获诸如Music/genre/rock
或Product/edit/5
之类的 URL。这两个的最终路由值将是:
The curlybraces indicate a kind of named wildcard.
The
"Music/Index"
route will only match the URLMusic/Index
and nothing elseThe
"Music/{Name}"
route will match any URLs starting with Music, and having anything after the slash. It will match both the URLsMusic/metallica
andMusic/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 likeMusic/genre/rock
orProduct/edit/5
.The resulting routevalues for these two will be:
我将尝试提供一个不那么做作的例子。
ASP.NET MVC 中的路由被放入字典中,当有传入请求时,MVC 管道会查看该请求并尝试确定将其路由到哪个控制器和操作。
假设我有以下控制器:
Home
、Forum
和Article
当我们这样做时,假设我有以下 控制器操作:
论坛
和文章
控制器上的查看
、编辑
、创建
。这些大括号允许我为两者创建一条路线:
这些大括号意味着它们放入的任何控制器(只要它是基于约束的
Article
或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
, andArticle
And while we're at it, let's say I have the following actions:
View
,Edit
,Create
on both theForum
andArticle
controllers.Those braces allow me to create one route for both:
Those braces mean that whatever controller they put in (as long as it's
Article
orForum
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:
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).