Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

If I simply create a folder, I get a 403 when browsing to http://mysite/Downloads. (Most likely because of directory browsing is disabled) But I want the MVC controller to kick in instead.

How do I do that?

like image 312
Magnus Johansson Avatar asked Sep 17 '09 08:09

Magnus Johansson


People also ask

Can we have multiple controllers with same name in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

Can we have two action methods with same name in MVC?

While ASP.NET MVC will allow you to have two actions with the same name, . NET won't allow you to have two methods with the same signature - i.e. the same name and parameters. You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.

What is App_Data folder in ASP NET MVC?

The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.

Can two different controllers access a single view in MVC?

Yes. Mention the view full path in the View method. If the name of your Views are same in both the controllers, You can keep the Common view under the Views/Shared directory and simply call the View method without any parameter. The View name should be same as the Action method name.


2 Answers

If you browse to http://mysite/Downloads/{ACTION} it will fire your controllers action.

The only thing that won't work in your example is the /Downloads with no action. You could re-write this URL to redirect you to your default action.

In addition, you will need to have the routehandler ignore your download files. You can add a line in your global.asax file to ignore all zip files or some other ignore pattern that suits.

routes.Ignore("{resource}.zip");
like image 155
Daniel Elliott Avatar answered Oct 05 '22 00:10

Daniel Elliott


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.

like image 42
rism Avatar answered Oct 05 '22 02:10

rism