1

在 ASP.NET 中,我想创建一个指向特定 Uri 的链接,并通过电子邮件将此链接发送给用户,例如http://www.BlaBla.com/CustomerPortal/Order/9876. /CustomerPortal/Order/9876我可以在代码隐藏中动态创建 Uri 的第二部分。我的问题是:如何http://www.BlaBla.com在我的应用程序中创建基本 Uri 而不对其进行硬编码?基本上我想要类似的东西:

http://localhost:1234/CustomerPortal/Order/9876 (on my development machine)
http://testserver/CustomerPortal/Order/9876 (on an internal test server)
http://www.BlaBla.com/CustomerPortal/Order/9876 (on the production server)

那么有没有办法询问应用程序在哪里运行的服务器:“请告诉我应用程序的基本 Uri”?还是有什么其他方式?

先感谢您!

4

3 回答 3

2

像这样的东西:

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/')
于 2010-06-12T11:23:08.177 回答
2

您必须在 config 中放置一个键,某处,因为当您考虑您的 Web 应用程序时,它并没有真正与 URL 绑定。例如:

http://localhost:1234/
http://yourMachineName:1234/
http://yourMachineName.domain.com:1234/
http://127.0.0.1:1234/

这些只是在...上访问同一站点的几种方法。localhost它是什么?生产环境中也存在同样的问题,几十个域或 IP 可能指向同一个 Web 应用程序,并且它使用主机标头或没有任何东西来区分它。关键是,当在请求的上下文之外时,站点并不真正知道它使用什么 URL,可能是任何东西,只是没有 1:1 的关系。

如果您在发送电子邮件处于请求的上下文中,那么请看一下HttpRequest.Url,这是一种Uri类型,您可以在此处查看可用的属性

你可以这样做:

var host = HttpContext.Current.Url.Host;
//generate your link using the host
于 2010-06-12T11:27:59.040 回答
1

怎么把它放进去web.config

<configuration>
    <appSettings>
       <add key="SendingUrlBase" value="http://www.BlaBla.com"/>
于 2010-06-12T11:02:02.377 回答