MVC application in subdirectory

Discussion in 'Site Programming, Development and Design' started by Bob C, Dec 17, 2019.

Tags:
  1. I am installing an ASP.NET MVC application in a subdirectory in my WinHost site. The user would access the app with:
    www.mysite.com/mysubdir.

    The app's start page loads ok, but after that, I find that the MVC routing only works if I code URL's with a prefix of /mysubdir/. For example in an <a> tag, or in a jQuery Ajax call, I need to use: /mysubdir/Home/NextPage.
    If I just use /Home/NextPage, it returns file not found.
    Interestingly, @Html.ActionLink("Some text", "NextPage") renders with the /mysubdir/ prefix as above.

    It seems as if it's starting from the root of the site, and navigating from there.

    Note: In the root of the site, in Web.config, I have wrapped the <system.web> section inside a <location> tag with this syntax:
    <location path="." inheritInChildApplications="false"> ... </location>

    My question: I am wondering if there is some setting in Web.config or global.asax or elsewhere that I should be using so that the routing is directed to the subdirectory without having to embed the subdirectory name in the URL's.
     
  2. I forgot to mention: I set the subdirectory as an application end point using the WinHost control panel.
     
  3. ComputerMan

    ComputerMan Winhost Staff

    Is the mysite.com the main domain name of the site account? Or is it a domain pointer to the site account and you want it to point to the subdirectory?
     
  4. mysite.com refers to the main domain name. I do not want a pointer to the subdirectory, I just want the user to access the application by entering www.mysite.com/mysubdir. (Note: I have not yet redirected my domain to WinHost servers, and am using the secondary URL supplied.)
     
  5. ComputerMan

    ComputerMan Winhost Staff

    Try this, rename the web.config file in the root to something completely different. Like web.config.bak

    Then go to your web application in the subdirectory. Does the issue still happen?
     
  6. ComputerMan

    ComputerMan Winhost Staff

    If the issue is resolved after you renamed the web.config file in the root. Then your web.config file is still passing down it's inheritance. Try wrapping the following <location path="." inheritInChildApplications="false"> ... </location> in different sections within the root's web.config file.
     
  7. I tried renaming web.config in the root, as you suggested, but I stillget the same behavior.
     
  8. I have done the following:

    1) Deleted everything in the root (except the subdirectory) in case something in there was the problem.

    2) Published another app to another subdirectory, MvcDemo. It is just Visual Studio's default MVC framework starter project. It shows the same behavior...

    Entering the following (using my secondary URL): http://zeemerix.w29.wh-2.com/MvcDemo loads the app correctly.
    But then, all links must be prefixed with the subdirectory name: for example: <a href="/mvcdemo/Home/About">About</a>. And also,
    @Html.ActionLink actually renders this syntax. That is:

    @Html.ActionLink("About", "About", "Home")

    renders as

    <a href="/mvcdemo/Home/About">About</a>
    Can you suggest anything else to try?
     
  9. Also, I created a third app, this time very simple, based on Visual Studio's "Empty" MVC framework project. I just added 2 views, Index and TestPage, and the former just has a few links to the latter. It's called "EmptyDemo", again in a subdirectory.

    Again, if you look at the page source, you see that @Html.ActionLink renders with the prefix "/EmptyDemo/Home/...".

    So all 3 apps show the same behavior.
    Can anyone think of anything that might be the problem?
     
  10. Elshadriel

    Elshadriel Winhost Staff

  11. ComputerMan

    ComputerMan Winhost Staff

  12. Thank you for looking at this issue.

    Yes, the About link in the menu bar works because it uses a Razor ActionLink, like so...
    @Html.ActionLink("About", "About", "Home")

    And the ActionLink, in the rendered HTML, strangely inserts the subdir prefix. Here is the rendered HTML. Why is /mvcdemo/ prefixed?
    <a href="/mvcdemo/Home/About">

    To see the 404
    If you want to see the 404, look at the second of the 3 "About" links at the bottom of the main page. If you look at the page source, it is hard coded as an <a> tag, rather than an ActionLink. It is coded as (I believe) it should be, without the subdir prefixed, but it results in a 404...
    <a href="/Home/About">

    The first of the 3 "About" links, on the other hand, does work, because I have prefixed the subdir, similar to what the ActionLink did. But whu should I have to do that?
    <a href="/mvcdemo/Home/About">
     
  13. ComputerMan

    ComputerMan Winhost Staff

    Is there a reason why you're placing the application in the subdirectory? I ask because it appears you want your URL to not have the subdirectory within it. If that is the case you need to do the redirect but the easiest would be to deploy the application to the root.
     
  14. I want the application in a subdirectory because I want to have several applications, and one domain.

    I do want the user to include the subdirectory when entering in a browser. For example:
    However the issue is in code. My understanding is that once the subdirectory is set as an application start point, then I should be able to just drop the application there, and then paths in code, like in an <a> tag, or in paths used in ajax calls, should be able to reference the Home controller with, say,
    <a href="/Home/About">

    But it seem that the path in code needs to have the subdiectory prefixed. Like this...
    <a href="/mvcdemo/Home/About">

    I guess I can work around that, but it complicates deployment somewhat, and I don't understand why it should be.
     
    Last edited: Dec 21, 2019
  15. Hi Bob,

    I know this is an old post, but wondering if you ever solved it. I have the exact same issue, and cannot figure out how to get the MVC App routing to work as it should. It seems to go to my root domain. If I copy the whole app over to the root domain, then it works fine. But in the subfolder, I can see that Ajax call, for example, is placing the controller action at the root not the root/sub-folder. Thanks in advance for your help.
     
  16. @Cary B Baer -- No I did not resolve it. I work around it by embedding the whole path (with the subfolder) in code. That works but it means using separate paths for local testing and deployment. Frustrating that Winhost help never seemed to grasp what the issue was. I think it is something in Winhost's configuration.
     
  17. Elshadriel

    Elshadriel Winhost Staff

    Well, the <a href> is just an HTML tag. MVC probably doesn't process that. That's probably why @Html.ActionLink("About", "About", "Home") works. MVC should convert that to the proper HTML markup (i.e. include the sub-directory in the markup). For it to ignore/include the sub-directory, you would probably have to change the default route as shown in this post. I'm not an MVC expert, but I assume where it's commented // URL with parameters is where you would put the sub-directory name.
     
  18. It's definitely something with WinHost. I transferred my site to GoDaddy, and everything works flawlessly. There is just something about Winhost and the use of sub-folders that screws up everything.
     

Share This Page