如何在 ASP.NET MVC 中映射 Post/[id]/Comments/[Action]?
我想为我的博客应用程序创建一个 RESTful API,但我不知道如何映射这样的控制器。
怎么办呢? Post 是否应该硬编码在 URL 模式中?
I want to create a RESTful API for my blog application but I can't tell how to map controllers like this.
How can it be done? Should Post be hardcoded in the URL pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以映射这样的路线:
硬编码 URL 没有任何问题,除非您添加的所有内容都需要硬编码 URL 并且它们变得无法维护。
我的理解是你希望 CommentsController 仅通过此路由调用。您不希望它被默认路由调用。您可以使用 IgnoreRoute 来实现这一点。
这将确保没有我们之前定义的路由就不会调用 CommentsController。我希望这就是你想要做的。
You could map a route like this:
There's nothing wrong with hardcoding URLs unless everything you add needs hardcoded URLs and they become unmaintainable.
What I understand is you want CommentsController called by only this route. You do not want it to be called by the default route. You can use
IgnoreRoute
for that.This will make sure CommentsController doesn't get called without the route we defined before. I hope that was what you were trying to do.