ASMX Web 服务中客户端应用程序和浏览器之间的区别?

发布于 2024-09-13 08:11:10 字数 931 浏览 2 评论 0原文

这是根据种类选择连接字符串的后续内容请求我没有得到答复,而我认为有效的却没有。

我有一个 Web 服务,需要根据用户从浏览器或客户端应用程序调用它来选择特定的连接字符串。

我尝试过:

HttpContext.Current != null? ConnectionStrings["Website"].ConnectionString : ConnectionStrings["Client"].ConnectionString

但意识到,在某些时候,即使我使用客户端应用程序,也有一些 HttpContext (如果有人可以解释为什么它会很棒),但 下的 Browser 字段请求“未知”。所以,然后我尝试了:

if ( HttpContext.Current != null )
{
  if ( HttpContext.Current.Request.Browser != "Unknown" )
  {
     //browser connection string here
  }
  else
     //client app connection string here
}
else
  //client app connection string here

这在调试时产生了奇迹,但在测试环境中,即使从客户端应用程序调用,它仍然指向浏览器连接字符串,就好像在某些时候浏览器不是“未知”......

是否有更容易/更简单的方法来做到这一点?我这样做的方式看起来真的很难看。

我现在很绝望,因为我不知道为什么会发生这种情况。

This is a follow-up to Choosing a Connection String based on kind of request for which I got no answer and what I thought worked doesn't.

I have a webservice that needs to choose a specific connection string based on the user calling it from a browser or from a client application.

I tried:

HttpContext.Current != null? ConnectionStrings["Website"].ConnectionString : ConnectionStrings["Client"].ConnectionString

but realized that at some point even if I'm using the client application, there is some HttpContext (if someone can explain why it'd be great) but the Browser field under Request is "Unknown". So, then I tried:

if ( HttpContext.Current != null )
{
  if ( HttpContext.Current.Request.Browser != "Unknown" )
  {
     //browser connection string here
  }
  else
     //client app connection string here
}
else
  //client app connection string here

This worked wonders when debugging, but on testing environment it still points to Browser connection string even when calling from the client app, as if at some point the Browser isn't "Unknown" ...

Is there a MUCH easier/simpler way to do this? The way I'm doing it seems really ugly.

I'm quite desperate at the moment as I have no idea why this is happening..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-09-20 08:11:10

不要检测和切换浏览器类型,而是考虑以下两个建议:

添加自定义请求标头

在各种调用方中,在 Http 请求中定义新的自定义标头。

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("CallerType", "ClientApp"); // "Browser", etc.

然后您就可以准确可靠地知道正在呼叫的客户类型。这很难出错,也不会被欺骗/错误。

在查询字符串中包含调用者类型

myService.asmx?BrowserType=1

将一个简单的新查询字符串参数添加到您的 .asmx webmethod 中。这在受控环境中的工作原理是一样的,但是如果其他用户/开发人员出错,或者预期值不正确,您就必须采取其他措施来纠正/处理。

两者都允许您轻松确定传入值的 connString。也许缺少修饰符/标题,您可以假设默认值。您的示例问题有 2 个基本结果,并且建议的解决方案都很容易扩展(浏览器、客户端应用程序、iPhone,等等)。

Rather than detecting and switching on the browser type, consider these two suggestions:

Add Custom Request Headers

In your various callers, define a new custom header in your Http request.

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("CallerType", "ClientApp"); // "Browser", etc.

Then you know exactly and reliably what type of client is calling. This would be hard to get wrong, and couldn't be spoofed/mistaken.

Include The Caller Type in the QueryString

myService.asmx?BrowserType=1

Add a simple new querystring parameter to your .asmx webmethod. This will work just the same in a controlled environment, but if other users/developers get it wrong, or malform the expected values, you'd have to take other measures to correct/handle.

Both allow you to easily determine the connString on the incoming value. Perhaps the absense of a modifier/header, you could assume a default. Your sample question has 2 basic outcomes, and either suggested solution will be easy to extend (browser, client app, iPhone, whathaveyou).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文