2

I'm wondering how it is possible to run an MVC application in a subdirectory. The problem is that when running in a subdirectory, every Action seems to be navigated to the Root path rather then the subdirectory.

In order to test if it would work, I've edited the "Project URL" and "Override application root URL" in the project properties window.

I've considered adding a key to the web.config with the configurable routing value and creating a singleton class that would read this key, although i would have to manually editing all pages to make it work, but clearly that wouldn't be the best way out there as it would take a lot of time and when willing to change the directory name, you'd have to change it over all the pages in the application.

Another thing I tried is editing the RouteConfig.cs file and replacing / adding it along with the default router.

routes.MapRoute(
            name: "Rooted",
            url: "{root}/{controller}/{action}/{id}",
            defaults:
                new { root = "Admin", controller = "Login", action = "Index", id = UrlParameter.Optional }
        );

Sadly this however didn't seem to work either as it would still redirect to Index/Home instead of Admin/Index/Home.

I'm wondering if someone has a better idea how to get this to work with as little change as possible. I can't be the first one to ask this, and when Googling about possible duplicates none came up with a suitable solution.

2 Answers 2

2

I don't know if is what you are looking for but I have a MVC project running under subdirectory of a main project. For every root I have to write in RouteConfig 2 rules. Consider that my subDirectory is sport.
Example for images

           routes.MapRoute(
                name: "Images",
                url: "files/images/{width}x{height}/{myID}.png",
                defaults: new { controller = "ControllerName", action = "ActionName" },
              constraints: new RouteValueDictionary { { "width", "[0-9]+" }, { "height", "[0-9]+" }, { "myID", "[0-9a-z_]+" }}
            );
            routes.MapRoute(
                name: "Images2",
                url: "sport/files/images/{width}x{height}/{myID}.png",
                defaults: new { controller = "ControllerName", action = "ActionName" },
              constraints: new RouteValueDictionary { { "width", "[0-9]+" }, { "height", "[0-9]+" }, { "myID", "[0-9a-z_]+" }}
            );

In your case if (for example) your subdirectory is test try in this way

1st rule for local

routes.MapRoute(
            name: "Rooted",
            url: "{controller}/{action}/{id}",
            defaults:
                new { controller = "Login", action = "Index", id = UrlParameter.Optional }
        );

2ns rule for pubblished

routes.MapRoute(
                name: "Rooted",
                url: "test/{controller}/{action}/{id}",
                defaults:
                    new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );

before doing this you should add a new Application (as subDirectory) to your site from IIS.

Follow these steps

  1. Open IIS Manager

  2. Right click on Default Web Site and select "Add Application"

  3. Set the Virtual Path "YourSubDireCtoryName" and and the proper Physical Path (Put all project files in a folder where you want; just write the correct path)

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is that it is still navigating to the root by default. A link would be '127.0.0.1:50000/Index/SomePage' instead of '127.0.0.1:50000/Admin/Index/SomePage'.
for as far all i can see.. your MapRoute is the same as mine. When testing ur 2nd rule for local (as I created a virtual map, it should work). It comes up with either one of these 2 errors: HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory. HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Did you add the SubDirectory as Application in your IIS?
Trying too, can't figure out how yet.
0

Turned out my problem was caused by something entirely different.

It went to the rootfolder because the form submit contained a hard path aka "/login/login" instead of "~/login/login".

Even though it was not the answer, I suggest people to look at faby's post too as it does contain some nice information which shouldn't get lost.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.