简体   繁体   中英

How to get the correct path in asp.net when deploying from localhost to a development server?

I have a simple web application that contains a folder called documents on the root of the website. Inside documents, is a file called test.doc. The path and file are stored in a database and when the web app is run, it reads in the values and creates the correct link to test.doc, in this case, it is http://localhost/documents/test.doc . The problem occurs when I publish to a folder on a development server. When I do that, my url is broken because it becomes something like this, http://development/testapp/documents/test.doc . This fails because it is looking for test.doc in http://development/documents/test.doc . I am not sure how to resolve this issue.

I am currently doing this in the markup, but I am unsure how to use ResolveUrl or ResolveClientUrl with it:

<a runat="server" href='<%# Eval("url") %>'><%#Eval("title") %></a>

尝试使用ResolveUrl函数正确映射。

ResolveUrl("~/documents/test.doc");

You can use the ResolveClientUrl method of the to properly map the urls. Take the url from the database, prepend the "~/" and pass to ResolveClientUrl.

I use a variable to hold the web root path. For production, it is just "/". For my dev machine, it is "/testapp/" (or whatever). I read this from an entry in the "appsettings" section in my web.config file. I use the variable to construct paths to pages, etc, like this:

string docPath = WebRootPath + "documents/test.doc"

Another answer is to use IIS instead of Personal Web Server (PWS-ie, the built VS.NET built in web server). Testing and debugging is much faster using IIS and it really doesn't take much work at all once you've gotten use to it. It also solves a lot of these issues that come about because of the different pathing scheme under PWS.

Download the IIS admin tool to allow you to create multiple websites with IIS 5.1 (XP).

Markup:

<a runat="server" href='<%# CustomResolveURL(Eval("url")) %>'><%#Eval("title") %></a>

Codebehind:

protected string CustomResolveURL(object obj)
{
    string result = string.empty;
    string url = (string)obj;
    if(!string.IsNullOrEmpty(url))
    {
        result = ResolveUrl("~/" + url);
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM