我想要一个条件("myBool")为真的html代码:
<div>
<fieldset>
<legend>
@sometext
</legend>
... other stuffs
</fieldset>
<div>
Run Code Online (Sandbox Code Playgroud)
当这是假的时候这个:
<div>
<b>
@sometext
</b>
... other stuffs
<div>
Run Code Online (Sandbox Code Playgroud)
我不想写两次相同的代码("其他东西"),所以我尝试了这个:
<div>
@if(myBool)
{
<fieldset>
<legend>
}
else
{
<b>
}
@sometext
if (myBool)
{
</legend>
}
else
{
</b>
}
...other stuff
if (myBool)
{
</fieldset>
}
</div>
Run Code Online (Sandbox Code Playgroud)
但我得到编译错误.
你知道我怎么能做我想做的事而不必做那样的事情:
@if(myBool)
{
<div>
<fieldset>
<legend>
@sometext
</legend>
... other stuffs
</fieldset>
<div>
}
else
{
<div>
<b>
@sometext
</b>
... other stuffs
<div>
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我写了以下服务:
namespace WebService1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string Test(string str)
{
if (string.IsNullOrEmpty(str))
throw new SoapException("message", SoapException.ClientFaultCode);
else
return str;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且有一个基本的应用程序来测试它(一个按钮调用Testclick事件的方法):
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1SoapClient ws = new WindowsFormsApplication1.ServiceReference1.Service1SoapClient();
try
{
ws.Test("");
}
catch (SoapException ex)
{
//I never go here
}
catch (FaultException ex)
{
//always go there
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我想抓住SoapException我的WebService抛出的东西,但是我总是去 …
我有一个名为A的抽象类,以及实现A的其他类(B,C,D,E,...)
我还有一个A对象列表.
我希望能够动态地将该列表中的每个对象强制转换为它们的"基础"类型(即B,C,D,...),以便能够在其他方法中调用它们的构造函数.
这是我现在所做的:
abstract class A { }
class B : A { }
class C : A { }
class D : A { }
class E : A { }
// ...
class Program
{
static void Main(string[] args)
{
List<A> list = new List<A> { new B(), new C(), new D(), new E() };
// ...
foreach (A item in list)
{
A obj = foo(item);
}
}
public static A foo(A obj)
{
if (obj.GetType() …Run Code Online (Sandbox Code Playgroud) 我为用户编写了一个动作,用于下载生成的xml,而不必将其写入服务器磁盘,但将其保存到内存中.
这是我的代码:
public FileContentResult MyAction()
{
MemoryStream myStream = new MemoryStream();
XDocument xml = GenerateXml(...);
xml.Save(myStream );
myStream .Position = 0;
return File(myStream.GetBuffer(), "text/xml", "myFile.xml");
}
Run Code Online (Sandbox Code Playgroud)
一切似乎工作正常,XML是正确的,我可以下载文件,但我不明白为什么我的文件末尾有691920"nul"caracters(这些caracters的数量似乎与长度有关)的xml):

它们来自哪里?我怎么能摆脱它们?
[更新]我试过这个:
public FileContentResult MyAction()
{
XDocument xml = GenerateXml(...);
byte[] bytes = new byte[xml.ToString().Length * sizeof(char)];
Buffer.BlockCopy(xml.ToString().ToCharArray(), 0, bytes, 0, bytes.Length);
return File(bytes, "text/xml", "myFile.xml");
}
Run Code Online (Sandbox Code Playgroud)
我没有得到"nul"角色.所以我想这是MemoryStream在文件末尾添加额外的caracters.所以在我的例子中,第二个代码解决了我的问题.
但我也生成了一个我无法阅读的Word文档(由于内容存在问题,因此无法打开xxx.docx).我想我在这里遇到同样的问题,内存流在文件末尾添加额外的caracters并破坏它.
c# ×3
asp.net-mvc ×2
asmx ×1
casting ×1
dynamic-cast ×1
exception ×1
html ×1
linq-to-xml ×1
memorystream ×1
razor ×1
reflection ×1
try-catch ×1