MVC 路由问题 - 尝试选取路由中连字符后的剩余部分
我创建了以下路由:
routes.MapRoute(
"Notes 1", // Route name
"{book}-{id}", // URL with parameters
new { controller = "Notes", action = "Note", id = UrlParameter.Optional }
);
使用此 URL 和 MVC 路由检查器:
http://127.0.0.1:81/java-abcd-efg
id < efg
book < java-abcd
controller < Notes
action < Note
我期望该书将包含“java”,而 id 将包含“abcd-efg”。有人可以解释一下出了什么问题以及为什么情况并非如此。
谢谢
I created the following route:
routes.MapRoute(
"Notes 1", // Route name
"{book}-{id}", // URL with parameters
new { controller = "Notes", action = "Note", id = UrlParameter.Optional }
);
Using this URL and the MVC route checker:
http://127.0.0.1:81/java-abcd-efg
id < efg
book < java-abcd
controller < Notes
action < Note
What I expected was that book would contain "java" and id would contain "abcd-efg". Can someone explain what's wrong and why this isn't the case.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是你没有告诉 MVC 期望不止一个破折号;所以它抓住了它发现的东西并尝试以这种方式解析它。
好吧,这对我们来说很糟糕。
如果您知道您的路线始终是:
那么我们可以处理它,但需要在服务器端进行解析。
相信我,使用斜杠会容易得多。如果有人来找你并抱怨 SEO,请让他们向你展示重要的确切位置(以及为什么斜杠不起作用)。我并不是说他们错了,但有很多“我认为 SEO 是这样工作的”,这很快就会让人厌烦。
我将为您提供两种解决方案。解决方案 1 将是我处理它(并争取)的方式,解决方案 2 将是针对您当前处理方案的特定问题的解决方案(我已经在实践中处理过解决方案 2,并且最多这非常令人讨厌)。
解决方案 1
设置您的路由,类似于 Stack Overflow 设置其路由的方式。这将使您的 SEO 人员拥有他神圣的 SEO,并且您将得到您想要的:没有溃疡。
您的网址将更改为如下所示:
http://example.com/books/id/book-type-name
然后,您可以使用 bookName 进行显示,但实际上不能使用它来获取任何内容代码。只需更改您的
Show
方法即可通过 ID 检索项目。解决方案 2
该解决方案使用 ActionFilter 来拦截请求并解析出内容是什么。这是一个令人讨厌的解决方案,但它应该有效。
我 写了一个答案,显示这是另一个问题的解决方案。这里也适用,只需更改搜索类型以根据 URL 中的内容查询数据库即可。另外,该路线必须位于路线的底部,否则它将首先捕获该路线。所有标准免责声明均适用:使用参数化查询,或为您参数化查询的 ORM。
The problem is that you didn't tell MVC to expect more than one dash; so it grabbed what it found and tried to parse it that way.
Well, that just sucks for us.
If you know that your route will always be:
Then we can handle that, but it's going to take work on the server side to parse that.
Believe me, it'd be much easier just to use slashes. If someone comes to you and complains about SEO, ask them to show you the exact place where it matters (And somehow why slashes don't work as well). I'm not saying they're wrong, but there's a lot of, "I think it works this way with SEO" and that gets annoying, fast.
I'm going to provide you with two solutions. Solution 1 would be the way I would handle it (and fight for) and solution 2 would be the solution to your particular problem with your current scheme for handling (and I've dealt with solution 2 in practice, and at best it is very icky).
Solution 1
Set up your routes similar to how Stack Overflow sets up theirs. This would allow your SEO guy to have his hallowed SEO, and you would get what you want: No ulcers.
Your URLs would change to look like:
http://example.com/books/id/book-type-name
Then, you can use the bookName for display, but not actually use it to get any code. Just change your
Show
method to retrieve items by their Id.Solution 2
This solution uses a ActionFilter to intercept the requests and parse out what's what. It's an icky solution, but it should work.
I wrote an answer that showed this solution for another question. It is applicable here as well, just change the search type to query the database based on what's in the URL. Also, the route will have to go at the bottom of your routes, otherwise it'll catch that first. All the standard disclaimers apply: Use parameretized queries, or an ORM that parameteretizes them for you.
看起来路由引擎首先从右手端进行评估。我必须说你的路线设计不太好。基本上,使用相同的字符来分割路线的各个部分以及在路线的单个部分内是一个很大的禁忌。您可以将连字符更改为其他内容吗?我会这样做:
这会产生这个(仍然可以在 id 中保留连字符):
It looks like the routing engine is evaluating from the right hand end first. I must say that your route design is not great. Basically having the same character to split parts of the route and also within a single part of the route is a big no-no. Can you change the hyphen for something else? I'd do it like this:
which would yield this (can still keep hyphen within id):