在Web和非Web应用程序之间共享HttpContext代码 [英] Share HttpContext code between web and non-web application

查看:93
本文介绍了在Web和非Web应用程序之间共享HttpContext代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB.NET模块,该模块从资源文件读取以正确的语言显示文本.这是我的问题-此代码在Web应用程序和非Web应用程序之间共享,在Web应用程序中,我使用System.Web.HttpContext来确定用户的首选语言,但是现在我的Windows应用程序甚至无法编译,因为它说未定义HttpContext(我已经尝试过为完​​整的命名空间添加导入-没有骰子).

I've got a VB.NET module that reads from a resource file to display text in the correct language. Here's my problem - this code is shared between a web application and a non-web application, In the web application, I'm using System.Web.HttpContext to determine the user's preferred language, but now my Windows app won't even compile, because it says HttpContext isn't defined (I've already tried adding an imports for the full namespace - no dice).

如果我无法解决该问题,我希望使用某种try/catch块,但这不会改变Windows应用程序不会在其中引用HttpContext的情况下进行编译.如果不将这段代码移到新文件中并且仅将其包含在Web应用程序中(我不拥有该应用程序,因此我宁愿不处理那些隐含的问题),是否还有另一种选择要处理?

I would love to use some kind of try/catch block if I can't otherwise work around it, but that doesn't change that the windows app won't compile with a reference to HttpContext in it. Without moving this chunk of code into a new file and including it only in the web application (I don't own that app, so I'd rather not deal with those implications), is there another choice I have to deal with this?

如果这没有任何意义,请告诉我,我会尽力澄清.

If it doesn't make sense, please let me know and I'll do my best to clarify.

解决方案::我刚刚添加了对System.Web的引用,该引用使我的应用程序得以编译.我还将HttpContext引用包装在"If HttpContext.Current is not Nothing Then ... End If"块中,这使它在未作为Web应用程序运行时跳过了代码,这正是我所要的.

SOLUTION: I just added a reference to System.Web, which allowed my application to compile. I also wrapped the HttpContext reference in an "If HttpContext.Current isnot Nothing Then...End If" block, which causes it to skip over the code if it's not running as a web application, which is exactly what I was looking for.

推荐答案

如果引用System.Web程序集,则应有权访问HttpContext.Current,该引用是对当前运行的Web应用程序的HttpContext对象的引用.如果该应用程序是普通的Win32应用程序,则此引用应为空指针.因此,在C#中,您将使用: if (HttpContext.Current == null) 或在VB中,您可以使用: If HttpContext.Current Is Nothing Then

If you reference the System.Web assembly, you should then be given access to HttpContext.Current, which is a reference to the currently running web app's HttpContext object. If the application is a normal Win32 app, this reference should be a null pointer. Thus in C# you would use: if (HttpContext.Current == null) or in VB you could use: If HttpContext.Current Is Nothing Then

但是,我从未尝试做这种事情,所以我不能保证结果.让我知道这是否适合您.

However, I have never tried doing something of this sort, so I can't guarantee the outcome. Let me know if that works for you.

谢谢, C

这篇关于在Web和非Web应用程序之间共享HttpContext代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆