简体   繁体   English

Clipboard.SetText()显示错误

[英]Clipboard.SetText() shows errors

I have been using a simple web form application for url shortening in asp.net. 我一直在使用一个简单的Web表单应用程序来缩短asp.net中的URL。 I have sucessfully made my function and when i change the url it should automatically copies to clipboard. 我已成功完成我的功能,当我更改网址时,它应自动复制到剪贴板。

I have made the text value to store in variable like 我已将文本值存储在变量中

   shortUrl.Text = shortURL; 

After this i am calling the Clipboard function 在此之后,我正在调用剪贴板功能

 clip_board(shortURL);

My function is 我的功能是

 public void clip_board(string shortUrl)
    {
       MessageBox.Show(shortUrl);
       Clipboard.SetText(shortUrl);

    }

Here the message box shows the shorten url but the Clipboard.SetText() showing error as 这里的消息框显示缩短的URL,但Clipboard.SetText()显示错误为

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. 在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。 Ensure that your Main function has STAThreadAttribute marked on it. 确保您的Main函数标记了STAThreadAttribute。

How to overcome this error? 如何克服这个错误?

You say it's an ASP.NET application: in this case your .NET code will be running on the server, so you can't access the client's clipboard - nor can you call WinForms methods such as MessageBox.Show . 你说它是一个ASP.NET应用程序:在这种情况下,你的.NET代码将在服务器上运行,因此你无法访问客户端的剪贴板 - 也无法调用WinBox等方法,如MessageBox.Show

In response to comment: 回应评论:

But it shows the message box with exact values ... 但它显示了具有确切值的消息框...

Are you debugging using Visual Studio and the Cassini web server? 您是否正在使用Visual Studio和Cassini Web服务器进行调试? In which case you might see the output of MessageBox.Show as it's running in the context of your current session. 在这种情况下,您可能会看到MessageBox.Show的输出,因为它在您当前会话的上下文中运行。 But this won't work when you deploy to IIS. 但是,当您部署到IIS时,这将不起作用。

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

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