简体   繁体   English

If语句在stringbuilder(WebMethod)中似乎不起作用

[英]If statement doesn't seem to work in a stringbuilder (WebMethod)

I have the following WebMethod that returns a conditional string. 我有下面的WebMethod返回一个条件字符串。 Unfortunately the if statement doesn't seem to work. 不幸的是,if语句似乎不起作用。 I know that the WebMethod is working because I get the Bla bla bla string but not the icons within the if statement. 我知道WebMethod正在工作,因为我得到了Bla bla bla字符串,但是没有if语句中的图标。 What am I doing wrong? 我究竟做错了什么?

[WebMethod]
public static string photos()
{
    StringBuilder photos_sb = new StringBuilder();
    photos_sb.AppendFormat("Bla bla bla bla...");
    db = Database.Open("DefaultConnection"); 
    var HasPhoto = db.Query("SELECT [IDphoto] FROM [photos]");
    if (HasPhoto != null)
    {
        photos_sb.AppendFormat("<img src=\"icon-Green.png\" />");
    }
    else
    {
        photos_sb.AppendFormat("<img src=\"icon-Gray.png\" />");
    }
    db.Close();
    db.Dispose();
    photos_sb.AppendFormat("Bla2 bla2 bla2 bla2...");

    return photos_sb.ToString();
}

Your img tags are missing their closing > . 您的img标签缺少结束符> This means that quite possibly they are being outputted but they are not coming out in the format you expected and thus rendering what you expect. 这意味着很有可能正在输出它们,但它们并没有以您期望的格式输出,因此无法呈现您期望的格式。 The code given will return something like: 给定的代码将返回如下内容:

Bla bla bla bla...<img src="icon-Gray.png"Bla2 bla2 bla2 bla2...

I would expect a browser to get very confused by this. 我希望浏览器对此感到非常困惑。

If this is in fact a typo and your outputted string really is just "Bla bla bla bla..." then your only possibility would seem to be that you are running the wrong version of your code. 如果这实际上是一个拼写错误,而您输出的字符串确实只是“ Bla bla bla bla ...”,那么您唯一的可能性似乎就是您运行的是错误的代码版本。 IF the above was compiled then it cannot return your value without going through the if statement and running at least one of the branches. 如果上面的代码被编译,那么它必须通过if语句并运行至少一个分支才能返回您的值。

I'd suggest three things: 我建议三件事:

  1. Ensure you are running the correct compiled code. 确保您正在运行正确的编译代码。
  2. Run through it with a debugger to trace its actual execution. 使用调试器运行它以跟踪其实际执行。
  3. Confirm exactly what your output string is from this method, not what you are seeing several layers of code down the line in whatever is consuming this method. 确切确认此方法的输出字符串是什么,而不是确认使用此方法的情况下您看到的是几层代码。

I think the answer has been provided already so this is just some helpful advice. 我认为已经提供了答案,所以这只是一些有用的建议。 Save yourself hassle and reduce repeating code: 节省您的麻烦并减少重复代码:

photos_sb.Append("Bla bla bla bla...");
[....]
photos_sb.Append("<img src=\"");
if (HasPhoto != null)
{
    photos_sb.Append("icon-Green.png");
}
else
{
    photos_sb.Append("icon-Gray.png");
}
photos_sb.Append("\"" />");

This way your condition is only changing the one bit of code that actually varies. 这样,您的条件只会更改实际变化的一小段代码。

A slightly cleaner way using AppendFormat would be something like this... 使用AppendFormat一种更AppendFormat方法是这样的...

photos_sb.AppendFormat("Bla bla bla bla...");
[....]

string photo = "icon-Gray.png";
if (HasPhoto != null)
{
    photo = "icon-Green.png";
}
photos_sb.AppendFormat("<img src=\"{0}\" />", photo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM