简体   繁体   English

使用Route属性无法解析URL.Action结果

[英]Url.Action result does not resolve using Route attribute

I'm rebuilding the front-end of an application and due to its complexity I have to work on the existing legacy business layer. 我正在重建应用程序的前端,由于其复杂性,我必须在现有的旧业务层上工作。 As a result, we have things called "News" and "Documents" but in reality both are "Documents" where it is stored. 结果,我们有了被称为“新闻”和“文档”的东西,但实际上两者都是存储它的“文档”。

I've made a DocumentsController which handles everything just fine, slapping a [Route("News/{action=index}")] and [Route("Documents/{action=index}")] on the controller allows me to refer to the controller as either News or Documents. 我已经制作了一个DocumentsController,它可以处理所有事情,在控制器上拍了[Route("News/{action=index}")][Route("Documents/{action=index}")]作为新闻或文档发送给控制器。 So far so good. 到现在为止还挺好。 Viewing a specific Document using a single ActionResult with the attributes [Route("Documents/View/{id}"] and [Route("News/View/{id}"] also works just fine. However I am running into an issue when I try to use anything other than id as the parameter but only for the News part. 使用具有[Route("Documents/View/{id}"][Route("News/View/{id}"]属性的单个ActionResult查看特定文档也可以,但是我遇到了一个问题当我尝试使用id以外的任何参数作为参数,但仅用于News部分时。

My ActionResult method has the following definition 我的ActionResult方法具有以下定义

[Route("Documents/Download/{documentGuid}/{attachmentGuid}")]
[Route("News/Download/{documentGuid}/{attachmentGuid}")]
public ActionResult Download(Guid documentGuid, Guid attachmentGuid)
...

And my View has the following to get the link 我的视图具有以下获取链接

<a href="@Url.Action("Download", "Documents", new { documentGuid = Model.Id, attachmentGuid = attachment.AttachmentId })">Download</a>

This will generate a link akin to site/Documents/Download/guid/guid perfectly whenever I have "Documents" as the controller, but if I put "News" there I get a URL generated that uses querystring akin to site/News/Download?guid&guid parameters and resolves into a 404. If I then manually remove the querystring tokens and format the URL manually it will resolve fine. 每当我将“文档”作为控制器时,这将完美地生成一个类似于site/Documents/Download/guid/guid的链接,但是如果我输入“新闻”,则会生成一个使用类似于site/News/Download?guid&guid查询字符串的URL site/News/Download?guid&guid参数并解析为404。如果我随后手动删除查询字符串标记并手动设置URL格式,则可以很好地解决。

What is going wrong here, is something conflicting that I am missing? 这里出了什么问题,我缺少一些矛盾之处?

The parameters of your Url.Action are the name of the controller and the name of the action, it only works for documents because by coincidence then your route corresponds to the right names. Url.Action的参数是控制器的名称和操作的名称,它仅适用于文档,因为碰巧您的路由与正确的名称相对应。 If you want to use a specific route you have to name your routes and then use one of the methods that takes the route name to construct it. 如果要使用特定路由,则必须命名路由,然后使用采用路由名称的方法之一来构造它。

When looking up a route on an incoming request, routing will use the URL to determine which route matches. 在传入请求中查找路由时,路由将使用URL确定匹配的路由。 Your incoming URLs are unique, so it works fine. 您输入的URL是唯一的,因此可以正常工作。

However, when looking up a route to generate, MVC will use the route values to determine which route matches. 但是,当查找要生成的路由时,MVC将使用路由值来确定匹配的路由。 The literal segments in the URL ( News/Download/ ) are completely ignored for this part of the process. 在此过程的这一部分,将完全忽略URL中的文字片段News/Download/ )。

When using attribute routing, the route values are derived from the controller name and action name of the method you have decorated. 使用属性路由时,路由值从您修饰的方法的控制器名称和操作名称派生。 So, in both cases, your route values are: 因此,在两种情况下,您的路线值为:

| Key             | Value           |
|-----------------|-----------------|
| controller      | Documents       |
| action          | Download        |
| documentGuid    | <some GUID>     |
| attachmentGuid  | <some GUID>     |

In other words, your route values are not unique . 换句话说,您的路线值不是唯一的 Therefore, the first match in the route table always wins. 因此,路由表中的第一个匹配项始终获胜。

To get around this problem, you can use named routes. 要解决此问题,可以使用命名路由。

[Route("Documents/Download/{documentGuid}/{attachmentGuid}", Name = "Documents")]
[Route("News/Download/{documentGuid}/{attachmentGuid}", Name = "News")]
public ActionResult Download(Guid documentGuid, Guid attachmentGuid)
...

Then, resolve the URLs with @Url.RouteUrl or @Html.RouteLink . 然后,使用@Url.RouteUrl@Html.RouteLink解析URL。

@Html.RouteLink("Download", "News", new { controller = "Documents", action = "Download", documentGuid = Model.Id, attachmentGuid = attachment.AttachmentId })

Or 要么

<a href="@Url.RouteUrl("News", new { controller = "Documents", action = "Download", documentGuid = Model.Id, attachmentGuid = attachment.AttachmentId })">Download</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM