URL的Asp.Net绝对路径 [英] Asp.Net Absolute Path of a URL

查看:310
本文介绍了URL的Asp.Net绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要更简单的web应用程序与不同服务器上的其他应用程序共享文件,我用我​​的母版页基地href标记。正如许多人所发现的,这打破表单的路径。我有一个工作表适配器类,但我不知道如何获取URL的绝对路径。目前,我的计划是很难coded到使用一个类似于:

To make it simpler for a webapp to share files with another app on a different server, I'm using a base href tag in my master page. As many people have discovered, this breaks webform paths. I have a working Form Adaptor class but am not sure how to get the absolute path of the url. Currently, my program is hardcoded to use something akin to :

HttpContext Context = HttpContext.Current;
value = "http://localhost" + Context.Request.RawUrl;

这是值得指出的是,我目前正在测试我的本地IIS服务器上,因此对很多事情我已经使用顺序尝试什么的绝对路径不包括域名(​​我的本地IIS一个奇怪的倾向外部是不可见)。这意味着它不是一个绝对路径,因而基本href会破坏它。

It is worth noting that I'm currently testing on my local IIS server, so there's a strange tendency for a lot of things I've tried using in order what the absolute path do not include the domain name (my local IIS is not visible externally). Which means it isn't an absolute path and thus the base href will wreck it.

有没有办法解决这个,这样它会在本地工作没有硬编码,但是当上传到服务器也将正常工作的好办法?我想preFER避免任何涉及做一些在服务器端副本不同。

Is there a good way to handle this such that it will work locally without hardcoding but will also work properly when uploaded to a server? I'd prefer to avoid anything that involves doing something different on the server-side copy.

是的,我知道我可以使用单独的web.config文件本地和服务器上得到这个信息,但是这是丑陋的,违反干燥。

Yes, I realize I could use separate web.config files locally and on the server to get this information but this is ugly and violates DRY.

推荐答案

我已经在过去使用这样的:

I have used this in the past:

// Gets the base url in the following format: 
// "http(s)://domain(:port)/AppPath"
HttpContext.Current.Request.Url.Scheme 
    + "://"
    + HttpContext.Current.Request.Url.Authority 
    + HttpContext.Current.Request.ApplicationPath;

这篇关于URL的Asp.Net绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆