How to have folder and controller with same name in ASP.NET MVC?

后端 未结 2 1686
盖世英雄少女心
盖世英雄少女心 2020-12-17 17:43

I have a MVC controller called Downloads. http://mysite/Downloads

I also want to put a physical file in a physical folder called http://mysite/Downloads/MyFile.zip.<

2条回答
  •  隐瞒了意图╮
    2020-12-17 18:10

    Since .NET 3.5, you can route existing files:

    public static void RegisterRoutes(RouteCollection routes) {
        routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
           name: "Default",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", 
                              action = "Index", 
                              id = UrlParameter.Optional }
        );
    }
    

    So suppose we had a folder on the site root called Markets containing an audio.mp3 file:

    \Markets
    \Markets\audio.mp3
    

    Assuming the existence of a MarketsController, if we made a request for Markets, it'd be routed to Markets/Index.

    If we requested /Markets/audio.mp3 we'd get the mp3 file and if we requested Markets/AnythingElse, normal routing would apply.

提交回复
热议问题