Search code examples
asp.netasp.net-mvcroutesurl-routingasp.net-mvc-routing

Make route work without specifying action name in URL. - MVC


I have the following controller within my MVC project:

public class PressController : Controller
    {
        // GET: Press
        public ActionResult Index()
        {
            return File("../press/FFF_PRESS.zip", ".zip");
        }
    }

My routes

 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 }
        );
    }

When I load the site my URL is as follows:

www.example.com

Which displays the home page correctly, when I click on the following Action <li class="footer__navEl"><a href='@Url.Action("Index", "Press")'>PRESS</a></li>

I would like the URL to be

www.example.com/press 

and return the zip file.

However when I click this Action I get the following:

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

Yet when I specify

www.example.com/press/index

The .zip file is returned correctly.

Now I added the following to my routes.config:

routes.MapRoute("Press", "Press", new { controller = "Press", action = "Index" });

I still get the same error mentioned above, can someone shed some light into what I might be missing to get this to perform correctly?


Solution

  • HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

    The error indicates you have a physical directory on your web server named press. What is happening is that the web server is returning the directory instead of passing the request on to MVC. The default setting in IIS is not to list the directory's contents, hence the error.

    You need to either delete the press directory (recommended), or reconfigure IIS to run the MVC module instead of the directory by using the runAllManagedModulesForAllRequests, which has some caveats.

    This isn't a routing problem at all - it is a webserver configuration problem.