【问题标题】:Ternary condition between HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.Url.Authority on aspx pageaspx 页面上 HttpContext.Current.Request.Url.Host 和 HttpContext.Current.Request.Url.Authority 之间的三元条件
【发布时间】:2017-07-05 16:51:27
【问题描述】:

如何在 .aspx 页面上的 HttpContext.Current.Request.Url.Host 和 HttpContext.Current.Request.Url.Authority 之间使用带有样式的三元条件 - url 标记(想直接检查是否为 localhost) ?

示例如下:

"div style='<%= "height: 1115px; background-image: url(\"" + "http://" + HttpContext.Current.Request.Url.Host.ToString() +"ImagePath"); background-position: bottom center; background-repeat: no-repeat;" %>'"

我想检查它是否是本地主机,那么它会占用权限,如果不是,那么它必须占用主机。

【问题讨论】:

  • 你能发一个你想要的样本吗?
  • @PatrickHofman - 从我的项目中添加了一个 div 标签。

标签: c# asp.net webforms .net-3.5


【解决方案1】:

这个 .aspx 代码(抱歉,它很长,我无法在不破坏语法的情况下将其拆分为多个):

<div style='<%= "height: 1115px; background-image: url(\"http://" + (HttpContext.Current.Request.Url.Host.ToString().Contains("localhost") ? HttpContext.Current.Request.Url.Authority : HttpContext.Current.Request.Url.Host ) + "/" + "path/to/image.jpg" + "\"); background-position: bottom center; background-repeat: no-repeat;" %>'>my_div_content</div>

会产生这个结果:

<div style="height: 1115px; background-image: url("http://lc.host.com/path/to/image.jpg"); background-position: bottom center; background-repeat: no-repeat;">my_div_content</div>

鉴于您的 ImagePath 变量包含图像的真实路径,您需要在 div 中替换此行:

+ "path/to/image.jpg" + 

用这个:

+ ImagePath + 

不要担心style="url(" 都包含",它应该可以正常工作。我在 Chrome 中确认了这一点。

HTH

【讨论】:

    【解决方案2】:

    伪代码:

    HttpContext.Current.Request.Url.Contains("localhost") ? ... : ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多