动作方法 MVC 中的多态性
我有两个操作:
//操作 1 public FileResult Download(string 文件夹, string 文件名) { ... }
//操作 2 public FileResult Download(int id, string fileName) { ... }
当我尝试下载以下 URL 时: http://localhost:54630/Downloads/Download/15?fileName=sharedhostinggsg。 pdf
发生错误: 当前对控制器类型“DownloadsController”上的操作“Download”的请求在以下操作方法之间不明确: SextaIgreja.Web.Controllers.DownloadsController 类型上的 System.Web.Mvc.FileResult Download(Int32) System.Web.Mvc.FileResult Download(System.String, System.String) on type SextaIgreja.Web.Controllers.DownloadsController
如何制作它们:
网址:../Downloads/Download/15?文件名=sharedhostinggsg.pdf 操作:操作 2
网址:../Downloads?folder=Documentos$fileName=xx.docx 操作:操作 1
我尝试对我的路线施加限制,但没有成功:
routes.MapRoute(
"Download", // Route name
"Downloads/Download/{id}", // URL with parameters
new { controller = "Downloads", action = "Download" }, // Parameter defaults
new { id = @"\d+" }
);
在互联网上搜索我发现了几个链接,但我不明白如何解决我的问题。 这个,例如找不到RequireRequestValue属性。我不知道它是哪个命名空间。
I have two actions:
//Action 1
public FileResult Download(string folder, string fileName) { ... }
//Action 2
public FileResult Download(int id, string fileName) { ... }
When I try to download the following URL:
http://localhost:54630/Downloads/Download/15?fileName=sharedhostinggsg.pdf
The error happens:
The current request for action 'Download' on controller type 'DownloadsController' is ambiguous between the following action methods:
System.Web.Mvc.FileResult Download(Int32) on type SextaIgreja.Web.Controllers.DownloadsController
System.Web.Mvc.FileResult Download(System.String, System.String) on type SextaIgreja.Web.Controllers.DownloadsController
How can I make them:
Url: ../Downloads/Download/15?fileName=sharedhostinggsg.pdf
Action: Action 2
Url: ../Downloads?folder=Documentos$fileName=xx.docx
Action: Action 1
I tried to put a constraint on my route, but did not work:
routes.MapRoute(
"Download", // Route name
"Downloads/Download/{id}", // URL with parameters
new { controller = "Downloads", action = "Download" }, // Parameter defaults
new { id = @"\d+" }
);
Searching the Internet I found several links but I could not understand how I can solve my problem. This, for example, the RequireRequestValue attribute is not found. I do not know which namespace it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提到的 RequireRequestValue 是他们创建的自定义类(来自发布的示例)所以你不会在任何 Microsoft 命名空间中找到它。
您将看到的类继承自 ActionMethodSelectorAttribute。该属性类可用于帮助过滤操作,就像 AcceptVerbs 属性一样。正如该链接的示例所示,它们返回 true 或 false 取决于路由参数中是否指定了值。
因此,根据您发布的示例,创建一个名为 RequireRequestValueAttribute 的类。然后像这样装饰你的两个下载操作方法:
The RequireRequestValue that you mention is a custom class they created (from Example posted)so you will not find it in any Microsoft namespace.
The class you will see inherits from ActionMethodSelectorAttribute. This attribute class can be used to help filter actions much like the AcceptVerbs attribute. So as in the example of that link they are returning true or false dependant on if a value is specified in the route arguments.
So following from that example you posted, create a class called RequireRequestValueAttribute. Then decorate your two Downloads action methods like so: