开发者

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 ht开发者_StackOverflow中文版tp://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>


Try to use ResolveUrl function to map correctly.

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-i.e, 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;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜