Stefan Chonov
Stefan Chonov

Reputation: 121

ASP.NET MVC Routing config issue

I have an application into application pool of IIS 7. I added a new virtual directory into the application. My application run on http://localhost.com/ and I would like run my virtual directory on http://localhost.com/VirtualDirectory/. The reason why i would like to do that is the virtual directory is used as application. I would like to use Virtual Directory as SubSite. My problem is that i can't access controllers and views under virtual directory. I access it when i change the return result of ActionResult method as "return View("/VirtualDirectory/Views/Home/Index.cshtml");". I don't wanna hard code the VirtualDirectory on every return Action result.

My Route config is:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                  name: "Home", // Route name
                  url: "{id}", // URL with parameters
                  defaults: new { controller = "Home", action = "Index" /*, id = ""*/ } // Parameter defaults
              );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Defaut", id = UrlParameter.Optional } // Parameter defaults
            );            
        }

I try to make the next change:

routes.MapRoute(
                    "Default", // Route name
                    "VirtualDirectory/{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Defaut", id = UrlParameter.Optional } // Parameter defaults
                );

But when i try to load http://localhost.com/VirtualDirectory I've got the message:

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

Upvotes: 0

Views: 886

Answers (1)

undefined
undefined

Reputation: 34238

You shouldnt need to change anything at all in the routing of your application to run in a virtual directory.

However you will still need to make sure you use relative paths (not absolute / based paths) throughout your application. This is the same with any technology. / always ignores the virtual directory.

Upvotes: 1

Related Questions