asp.net-mvcserver-side-includes

Relative Server-side include and ASP.NET MVC


I have an ASP.NET MVC Web application that needs to include several other html files using the include directive, like so:

<!--#include virtual=/include/bottom.shtml -->

My problem is that I will need to deploy this application on servers where the application path will differ. For example, in one place I might have the application mapped on, say, localhost/myapp/ and in another I might have just localhost/.

In the localhost/ scenario, the code example I gave above works just fine. But when my app is on localhost/myapp, I get a parser error message which lets me know it could not find the file.

How can I customize the directive to take into account the relative path of the application (preferably without using find and replace every time) ?

Thank you!


Solution

  • Kill the leading slash -- that is forcing the path to calculate from the root, not your page.

    But like @mare said, there is no reason to be using a #include in 2011. What you probably want to do is render it as a partial using Razor's @Html.Partial().


    Given the updated constraints, there is still a much cleaner way to handle this in MVC than using the old #include. What you want to do is setup a controller action to read the file off disk and spit back the content. Then render that action like a normal MVC action.