是否可以将某些 ASMX Web 服务方法限制为仅 GET 或 POST?

发布于 2024-08-01 14:54:18 字数 404 浏览 1 评论 0原文

不幸的是,我正在对一个使用 ASP.NET Web 窗体构建的(遗留)项目进行一些工作。 我无法将其迁移到 ASP.NET MVC(所以请不要建议将其作为答案)。

我希望创建一个 API 来公开我们的一些数据。 所以,我决定制作一个网络服务。 很简单的东西。 但是,对于前几个方法,我希望仅将它们公开为 HTTP-GET。 对于我的最后一个方法,我希望仅将其公开为 HTTP-POST。

我已经习惯使用 ASP.NET MVC,这就是为什么我真的很喜欢这种 RESTful 编程方式,但我不确定如何使用我的 Web 服务来做到这一点?

我正在使用 URL 重写器,以便处理 api 的“RESTfull”消耗(它很蹩脚,但它有效)。

那么,有没有办法阻止 Web 服务只接受 HTTP 动词呢?

Unfortunately, I'm doing some work on a (legacy) project that is build with ASP.NET Web Forms. I can't migrate it to ASP.NET MVC (so please don't suggest that as an answer).

I wish to create an API to expose some of our data. So, I went a head and made a web service. Pretty simple stuff. BUT, for the first few methods, I wish to expose them only as HTTP-GET. For my last method, I wish to expose this only as HTTP-POST.

I'm so used to using ASP.NET MVC, which is why I really love this RESTful way of programming, but I'm not sure how to do this with my web service?

I'm using a URL Rewriter, so that handles the 'RESTfull' consuming of the api (it's lame, but it works).

So, is there a way to block a web service to only accept the HTTP verb?

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

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

发布评论

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

评论(3

我们只是彼此的过ke 2024-08-08 14:54:18

您可以在 web.config 中启用 GET,方法是:

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

要将单个方法限制为 POST,您可以在运行时测试 HttpContext.Current.Request.HttpMethod。

You can enable GET in the web.config, using:

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

To restrict a single method to POST, you can test HttpContext.Current.Request.HttpMethod at runtime.

行雁书 2024-08-08 14:54:18

我很确定 .net Web 服务类只接受发布的变量。

I'm pretty sure the .net web service class accepts only posted variables.

痕至 2024-08-08 14:54:18

在方法声明上方添加以下内容:

[ScriptMethod(UseHttpGet = false)]

这会强制该方法仅响应 POST。 如果您希望该方法响应 GET,请设置为 true。 这里的问题是您需要使用 ASP.NET 3.5 或 ASP.NET AJAX Extensions for ASP.NET 2.0。 您可以在此处阅读有关此内容的更多信息

Add the following above the method declaration:

[ScriptMethod(UseHttpGet = false)]

This forces the method to respond only to POST. Set to true if you want the method to respond to GET. The gotcha here is that you need to be using ASP.NET 3.5 or the ASP.NET AJAX Extensions for ASP.NET 2.0. You can read more about this here

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