简体   繁体   中英

Ternary condition between HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.Url.Authority on aspx page

How to use ternary condition with style - url tag, between HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.Url.Authority on .aspx page to (want to check is it localhost or not directly)?

Here is example:

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

I want to check if it is localhost then it will take Authority and if not then it must take Host.

this .aspx code (sorry, it's long, I couldn't break it into multiple without breaking syntax):

<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>

will produce this result:

<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>

given that your ImagePath variabl contains the real path to your image, you need to replace this line in the div:

+ "path/to/image.jpg" + 

with this:

+ ImagePath + 

Don't worry that both style=" and url(" contain " , it should work anyways. I confirmed this in Chrome.

HTH

Pseudo code:

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

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