是否可以拥有URL的控制器/视图:
http://mydomain/SomeFolder/
用于将文件放入其中的类似物理文件夹
http://mydomain/SomeFolder/*.*
目前网址http://mydomain/SomeFolder/
,只返回浏览的物理文件夹,如果web.config允许,但我希望它返回视图并http://mydomain/SomeFolder/*.*
返回文件夹中包含的文件。 / p>
答案 0 :(得分:1)
是的,这是可能的。假设您有~/SomeFolder/foo.png
个文件和~/Controllers/SomeFolderController
。您需要做的就是将RouteExistingFiles
属性设置为true:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.RouteExistingFiles = true;
}
现在,当您导航到/SomeFolder
或/SomeFolder/Index
时,将呈现Index
控制器的SomeFolder
操作。当您导航到/SomeFolder/foo.png
时,将提供静态文件。
答案 1 :(得分:0)
这是我找到的解决方案,但是我发现了一些更简单的方法。
使用此解决方案,我仍然无法将文件放在http://mydomain/SomeFolder/
的物理路径中,此文件夹不能以物理方式存在,否则控制器/视图不起作用。
但它会实现的是http://mydomain/SomeFolder/SomeFile.zip
返回与http://mydomain/**Content**/SomeFolder/SomeFile.zip
相同的文件,这是我的主要目标。
我需要为http://mydomain/SomeFolder/
和http://mydomain/SomeFolder/anything/file/etc
routes.MapRoute(
"SomeFolder",
"SomeFolder",
new { controller = "SomeFolder", action = "Index" }
);
routes.MapRoute(
"SomeFolderSub",
"SomeFolder/{*any}",
new { controller = "TryDownloadFile", action = "Index" }
);
routes.RouteExistingFiles
仍为false
。
然后我有SomeFolderController
返回SomeFolder的视图。
并TryDownloadFileController
检查文件是否存在http://mydomain/Content/SomeFolder/
而不是http://mydomain/SomeFolder/
并返回。
(我想如果该文件不存在,它应该返回一些404响应。这将是return this.HttpNotFound();
)