开发者

.net web application change root directory

开发者 https://www.devze.com 2023-02-20 03:32 出处:网络
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 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 f开发者_如何学Ciles 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>


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>


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).


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消