是否可以将某些 ASMX Web 服务方法限制为仅 GET 或 POST?
不幸的是,我正在对一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 web.config 中启用 GET,方法是:
要将单个方法限制为 POST,您可以在运行时测试 HttpContext.Current.Request.HttpMethod。
You can enable GET in the web.config, using:
To restrict a single method to POST, you can test HttpContext.Current.Request.HttpMethod at runtime.
我很确定 .net Web 服务类只接受发布的变量。
I'm pretty sure the .net web service class accepts only posted variables.
在方法声明上方添加以下内容:
这会强制该方法仅响应 POST。 如果您希望该方法响应 GET,请设置为 true。 这里的问题是您需要使用 ASP.NET 3.5 或 ASP.NET AJAX Extensions for ASP.NET 2.0。 您可以在此处阅读有关此内容的更多信息
Add the following above the method declaration:
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