如何使用 MVC 路由特殊参数?

发布于 2024-10-10 16:59:13 字数 971 浏览 3 评论 0原文

我知道特殊参数 {controller}{action},但我见过博客/帖子抛出特殊参数,例如 {controller} /{action}/{*id}。除了星号之外,还可以使用哪些其他特殊字符?我将如何使用它们?

最重要的是,我对如何使用路线约束感到困惑。对我来说有意义的常见映射是

routes.MapRoute( "Blog",
                 "Blog/{d}/{m}/{y}",
                 new { controller = "Blog", action = "Post" },
                 new { d = @"\d{1,2}", m = @"\d{1,2}", y = @"\d{4}" }
);

在约束对象中,dmy 都对应于 URL 中的参数,这是有道理的。不过,在我读过的一些博客上,当人们开始对不存在的参数施加约束时,我会感到困惑,例如

contentType = new ContentTypeConstraint(ConstraintContentType.JSON)

IsRootAction = new IsRootActionConstraint()

定义了哪些对象 contentTypeIsRootAction ?除了这两个属性之外,还有其他属性吗?

更新 我做了更多搜索,发现在底层 new {controller = "Blog"} 被转换为 RouteValueDictionary。这基本上只是简写。我很好奇的参数(IsRootAction 和 ContentType)只是字典的键名,仅此而已。

I'm aware of the special parameters {controller} and {action}, but I've seen blogs/posts throw in special parameters such as {controller}/{action}/{*id}. Besides the asterisk, what other special characters are available to use, and how would I use them?

On top of that, I'm confused on how to use route constraints. A common map that makes sense to me is

routes.MapRoute( "Blog",
                 "Blog/{d}/{m}/{y}",
                 new { controller = "Blog", action = "Post" },
                 new { d = @"\d{1,2}", m = @"\d{1,2}", y = @"\d{4}" }
);

In the constraint object, the d, m and y all correspond to the parameters within the URL, which makes sense. On some blogs I've read though, I get thrown off when people start throwing in constraints on parameters which don't exist such as

contentType = new ContentTypeConstraint(ConstraintContentType.JSON)

or

IsRootAction = new IsRootActionConstraint()

Which object are contentType and IsRootAction defined? Are there any more properties besides these two?

UPDATE
I've done some more searching, and I've found out that under the hood a new { controller = "Blog"} is converted into a RouteValueDictionary. This is basically just shorthand. The parameters I was curious about (IsRootAction and ContentType) are just key names for the dictionary, nothing more.

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

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

发布评论

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

评论(1

忘你却要生生世世 2024-10-17 16:59:13

操作约束要么在 MVC 框架中定义,要么由您的代码(最好在模型中)定义并实现 MVC 接口 IRouteConstraint。

有关如何定义操作约束的示例,请参阅此答案,这专门针对您有关 IsRootActionContraint 的问题。

在使用简单的 MVC 路由时遇到问题

我没有看到 {*...} 符号,一般语法是每个大括号名称都是从匹配 URL 中提取的参数。

Action Constraints are either defined in the MVC framework, or defined by your code (preferably in Models) and implement the MVC interface IRouteConstraint.

See this answer for an example of how an Action Constraint is defined, this speaks specifically to your question about IsRootActionContraint.

Having trouble with a simple MVC route

I have not seen the {*...} notation, the general syntax is that each curly-parenthesized name is an extracted parameter from the matching URL.

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