是否有可能限制某些ASMX Web服务方法为GET或POST只? [英] Is it possible to restrict certain ASMX web service methods to GET or POST only?

查看:247
本文介绍了是否有可能限制某些ASMX Web服务方法为GET或POST只?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我在做一个(传统)项目,是构建ASP.NET Web窗体一些工作。我不能把它移植到ASP.NET MVC(所以请不要认为作为一个答案)。

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).

我想创建一个API来暴露我们的一些数据。于是,我去了一个头,并提出了Web服务。 pretty简单的东西。但是,对于最初的几个方法,我要揭露他们只能作为HTTP-GET。对于我的最后一个方法,我想公开此仅作为HTTP-POST。

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.

我很习惯使用ASP.NET MVC,这就是为什么我真的很喜欢这种编程RESTful方式,但我不知道如何与我的web服务做到这一点?

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?

我使用的是URL重写,这样处理的API(这是跛,但它的作品)。

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

那么,有没有办法来阻止网络服务,只接受HTTP动词?

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

推荐答案

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

Add the following above the method declaration:

[ScriptMethod(UseHttpGet = false)]

这迫使只响应POST方法。如果你想方法来应对GET设置为true。这里的疑难杂症的是,你需要使用ASP.NET 3.5或ASP.NET AJAX扩展为ASP.NET 2.0。你可以阅读更多关于此<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.usehttpget.aspx\"相对=nofollow>这里

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

这篇关于是否有可能限制某些ASMX Web服务方法为GET或POST只?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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