0

I'm used to Apache/PHP where I could specify for a given domain where to look for the initial index.php file.

I'm doing a project in .Net where I have a directory structure something link

ApplicationName /Content/ -> Images stuff like that /Scripts/ /WebPages/

When I start the application, it gives me the above directory structure, but the actual starting place for the website is within the WebPages directory, a default.aspx page.

I didn't realize this was an issue until I started linking pages together and realized I didn't want to be including the /WebPages/ directory in the URL obv. somedomain.com/WebPages/somepasge.aspx.

Coming from Apache, what I'm looking for is a way to tell .Net where the root folder for the actual web directory is, which will be honored for relative links. I could move all of my files in the /WebPagse/ directory down, but I'd rather keep those separate in terms of making the directory structure pleasant and my OCD at bay.

Summary

Current Page Example...

example_domain.com/WebPages/SubDirectory/Subpage.aspx

Ideally would be linked via...

<a href='/SubDirectory/Subpage.aspx'>Some page</a>
Steffan
  • 307
  • 5
  • 16

5 Answers5

2

The root of my websites are in a sub folder www (where the index.html is). In order to set the root directory to www you can add the following to your Web.config file

<configuration>
    <system.web>
        ...
        <urlMappings enabled="true">
             <add url="~/" mappedUrl="~/www/index.html" />
        </urlMappings>
    </system.web>
    ...
    <system.webServer>
        ...
        <rewrite>
          <rules>
            <rule name="Redirect everything to root" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="/www/{R:0}" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
    ...
</configuration>
Chris Aelbrecht
  • 2,051
  • 21
  • 25
1

For server side controls you can use the tilde "~" to resolve the root. For non server controls, it's just plain xml paths (e.g. "../").

So for a client side image, you need to be aware of where you are, e.g.:

<int src="../Content/Images/image1.jpg"/>

for server side image:

<asp:Image ID="Image1" runat="server" ImageUrl="~/Content/Images/image1.jpg"/>

EDIT

I misunderstood the question the first time, though I'll leave the above in case it helps someone.

You can accomplish what you're looking for via URL Rewriting. This is new to ASP.NET Web Forms (version 4) and has been with ASP.NET MVC since version 1.

Note that if you're using an older version of ASP.NET you can still do URL rewriting, but not with the version baked into the .NET 4.

In your case, you can essentially match on "/WebPages/*.aspx", figure out the subdirectories and the page and then rewrite from there.

Advice

If you're going to go down the rewrite path, I suggest you just come up with nicer paths (no .aspx extension, no query string parameters, etc).

Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78
  • Okay that's fine for things under the Content folder, I get that. How about my webpages however which are under the /WebPages/ directory, which I don't want to show. I think I need to change my virtual directory root path or some such, and I'd ideally be able to do this in visual studio as well as my dev server (IIS 7) – Steffan Mar 24 '11 at 20:52
  • Ahhhh, I misunderstood your question. What version of .NET are you using? – Giovanni Galbo Mar 24 '11 at 20:57
  • I think 4.0, IIS 7 on my dev server that I publish to, 2010 Visual Studio that I develop on and use with the "Visual Studio Development Server". – Steffan Mar 24 '11 at 21:07
0

I'm guessing you need to put a tilde in front of your links like...

"~/subdirectory/subpage.aspx"

that means it will start from the virtual directory.

gt124
  • 1,238
  • 13
  • 23
  • I think I need to change my virtual directory as it's set to / in my visual studio configuration. It should technically be set to something like /WebPages/ however, when I tried that it meant my pages resolved to /WebPages/Webpages/ so that wasn't quite right... – Steffan Mar 24 '11 at 20:55
  • If your virtual directory is on example_domain.com (like it should) then your path should be ~/WebPages/subpage.aspx. If you're using MVC in the view you can use html helpers. If you are in webforms you should use an UrlHelper class, google that. Then use that from your page like <%= Urlhelper(EnumPage.Subpage) %> or whatever. Suggested below there are newer ways copied off mvc to do this in webforms, I'm out of the loop, I upgrade everything to MVC now. – gt124 Mar 24 '11 at 21:09
0

If you are using IIS, then set the Home Directory (Right click website -> Properties -> Home Directory) of the web site to WebPages. Then make virtual directories to your resources folders.

Thomas Li
  • 3,358
  • 18
  • 14
  • I can set that on the machine I am publishing to, is there a way to set this in my local Visual Studio environment for the server it sets up when I test my code? – Steffan Mar 24 '11 at 19:54
  • You can try using IIS Express and figure out the configuration needed to do what you want to do. Info about virtual directory here: http://stackoverflow.com/questions/4731608/iis-7-5-express-new-virtual-dir – Thomas Li Mar 24 '11 at 19:57
0

As per Giovanni's answer, you should use '~' syntax for all server side control sub-properties. The tilde (~) will resolve to the web application root.

However, for non-server control properties (stylesheet-link's, a-href's, img-src's etc) you would have to use relative paths.

For sake of consistency you could consider a custom HttpHandler implementation to resolve out '~' paths from non-server control content. There are probably a few examples around.

Community
  • 1
  • 1
Reddog
  • 15,219
  • 3
  • 51
  • 63