【发布时间】:2016-11-14 19:29:59
【问题描述】:
我正在重建应用程序的前端,由于其复杂性,我必须处理现有的遗留业务层。因此,我们有称为“新闻”和“文档”的东西,但实际上两者都是存储它的“文档”。
我已经制作了一个 DocumentsController,它可以很好地处理所有事情,在控制器上添加 [Route("News/{action=index}")] 和 [Route("Documents/{action=index}")] 可以让我将控制器称为新闻或文档。到目前为止,一切都很好。使用具有 [Route("Documents/View/{id}"] 和 [Route("News/View/{id}"] 属性的单个 ActionResult 查看特定文档也可以正常工作。但是,当我尝试使用除 id 以外的任何参数但仅用于新闻部分时,我遇到了问题。
我的ActionResult 方法有如下定义
[Route("Documents/Download/{documentGuid}/{attachmentGuid}")]
[Route("News/Download/{documentGuid}/{attachmentGuid}")]
public ActionResult Download(Guid documentGuid, Guid attachmentGuid)
...
而我的视图有以下获取链接
<a href="@Url.Action("Download", "Documents", new { documentGuid = Model.Id, attachmentGuid = attachment.AttachmentId })">Download</a>
每当我将“文档”作为控制器时,这将完美地生成类似于site/Documents/Download/guid/guid 的链接,但是如果我将“新闻”放在那里,我会生成一个使用类似于site/News/Download?guid&guid 参数的查询字符串并解析为404. 如果我随后手动删除查询字符串标记并手动格式化 URL,它将很好地解决。
这里出了什么问题,我错过了什么冲突吗?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing attributerouting