路由与反射问题
我在解决方案中添加了路由,以便在地址栏中提供更加用户友好的 URL。
我启动解决方案,当我滚动我的收藏夹链接时,我看到 URL .../Affaire/Favorite(如下图)。这个对我来说还可以。
当我滚动回收站链接时,我看到 URL ../Affaire/Deleted< /strong>(下图)。这个对我来说还可以。
然后我单击回收站链接,导航到相应的页面和地址中显示的 URL酒吧对我来说还可以(下图)。
接下来,我再次滚动“收藏夹”链接(下图),我看到 URL .. /Affaire/Delete?OnlyFavorite=true!!那不行。
路由现在不是从我的链接而是从活动 URL 检索属性!该属性名为 OnlyFavorite,但我不需要该属性。这就是“反射”。请注意,我的所有路由都使用相同的控制器和相同的操作,但使用不同的路由属性。
以下是我使用的一些链接。
导航到收藏夹页面的示例:
@Html.ActionLink("Favorites", "SearchAffaires", new { OnlyFavorite = true })
导航到回收站页面的示例:
@Html.ActionLink("Recycle bin", "SearchAffaires", new { StatusCode = "DELETED" })
以下是我的路线:
routes.MapRoute(
"Affaire Status Open/Closed/Deleted", // Route name
"{controller}/{StatusCode}", // URL with parameters
new { action = "SearchAffaires" }, // Parameter defaults
new { controller = "Affaire", StatusCode = "^Open$|^Closed$|^Deleted$" }// Contraints
);
routes.MapRoute(
"Affaire Only Favorite", // Route name
"{controller}/Favorite", // URL with parameters
new { action = "SearchAffaires", Page = 1, OnlyFavorite = true }, // Parameter defaults
new { controller = "Affaire" } // Contraints
);
您知道如何避免这种行为吗?
我不希望路由通过反射从我当前的 URL 获取名为 OnlyFavorite 的属性。我已经尝试在操作链接上传递 OnlyFavorite=null 但它不起作用:路由显示“好吧,我在链接本身上没有 OnlyFavorite 的值,但我在 URL 上有 OnlyFavorite,所以我使用它!”。
I added routing to my solution in order to have a more user friendly URL in the address bar.
I start the solution and when I rollover my Favorites link, I see the URL .../Affaire/Favorite (picture below). This one is OK for me.
When I rollover my Recycle bin link, I see the URL ../Affaire/Deleted (picture below). This one is OK for me.
Then I click on the Recycle bin link, I navigate to the corresponding page and the URL showed in the address bar is OK for me (picture below).
Next, I rollover the Favorite link again (picture below), I see the URL ../Affaire/Delete?OnlyFavorite=true!! That's not OK.
The routing is now retrieving an attribute not from my link but from the active URL! This attribute is named OnlyFavorite and I don't want this attribute. This is the "reflexion". Notice that all of my routes are using the same controller and the same action but using different attributes for the routes.
Below are some links I used.
Example for navigating to the favorite page:
@Html.ActionLink("Favorites", "SearchAffaires", new { OnlyFavorite = true })
Example for navigating to the recycle bin page:
@Html.ActionLink("Recycle bin", "SearchAffaires", new { StatusCode = "DELETED" })
Here are my routes:
routes.MapRoute(
"Affaire Status Open/Closed/Deleted", // Route name
"{controller}/{StatusCode}", // URL with parameters
new { action = "SearchAffaires" }, // Parameter defaults
new { controller = "Affaire", StatusCode = "^Open$|^Closed$|^Deleted$" }// Contraints
);
routes.MapRoute(
"Affaire Only Favorite", // Route name
"{controller}/Favorite", // URL with parameters
new { action = "SearchAffaires", Page = 1, OnlyFavorite = true }, // Parameter defaults
new { controller = "Affaire" } // Contraints
);
Do you have any idea how can I proceed to avoid this behaviour?
I don't want the routing to get the attribute named OnlyFavorite from my current URL by reflexion. I already try to pass OnlyFavorite=null on the action link but it doesn't work: the routing says "ok, I don't have a value for OnlyFavorite on the link itself but I have OnlyFavorite on the URL so I use it!".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您位于
Deleted
页面时,链接将以这种方式处理,因为StatusCode
标记等于“Deleted”,因此满足第一个路由。按如下方式更改您的链接:已更新
最好的解决方案是反转您的路线。作为一般规则,最具体的路线应始终先行,然后您应该有更通用的路线。由于“Affaire Only 最喜欢的”路线更具体,因此应该始终先行。如果这是满足的第一条路线,那么应该可以解决您的问题。
更新 #2
我运行了一个测试,当我按如下方式设置路由时,所有链接都正确生成:
此外,以下更通用的路由也正确生成:
对于更通用的路由工作中,我必须将操作重命名为
Search
,并且必须将每个链接从 SearchAffaires 更改为 Search。When you are on the
Deleted
page, the link is being processed that way because theStatusCode
token is equal to Deleted, so the first route is satisfied. Change your link as follows:UPDATED
The best solution is to reverse your routes. As a general rule, the most specific routes should always go first, and you should have more generic routes later. Since the "Affaire Only Favorite" route is more specific, it should always go first. If it is the first route satisfied, that should address your issue.
UPDATE #2
I ran a test, and all of the links were generated correctly, when I set the routes as follows:
In addition, the following more generic routes also generated correctly:
For the more generic routes to work, I had to rename the action as
Search
and I had to change each link from SearchAffaires to Search.好吧,您可以使用 Html.RouteLink 帮助器来准确指向所使用的路线。但是,正如 @counsellorben 指出的那样,您应该将更具体的路线设置为第一个路线。
如果显示您的网址(如“Affaire/Favorite/True”)没有问题,那么您可以使用下面的示例:
Well, you could use Html.RouteLink helper to point exactly to the route used. However, as @counsellorben pointed out, you should set your more specific route to be the first one.
And if there's no problem to show your url like "Affaire/Favorite/True", then you can use the example below: