【问题标题】:How to convert asmx service to rest to invoke with URL如何将 asmx 服务转换为 rest 以使用 URL 调用
【发布时间】:2015-02-05 06:33:13
【问题描述】:

我已经创建了asmx服务,如何通过“URL”调用它?

这是我的代码:

[WebMethod]
public string start(string id, string name)
{
   string newname = name;
   return newname;
}

这是我当前的网址:

url = "http://localhost:XXXXX/WebService.asmx/start?id=" +id+ "&name="+name;

这是我的“Web.config”:

<configuration>
   <system.web>
     <compilation debug="true" targetFramework="4.5">
     </compilation>
    <httpRuntime maxRequestLength="1000000"/>
<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>
  <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
   <system.webServer>
     <directoryBrowse enabled="true"/>
   </system.webServer>
</configuration>

【问题讨论】:

    标签: c# asp.net web-services rest


    【解决方案1】:

    您可以装饰您的方法以允许 HTTP GET 请求

     [WebMethod]  
        [ScriptMethod(UseHttpGet=true)]
        public string start(string id, string name)
        {
           string newname = name;
           return newname;
        }
    

    并在 web.config 文件中执行以下操作

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

    将 UseHttpGet 属性设置为 true 可能会带来安全风险 如果您正在处理敏感数据或您的应用程序 交易。在 GET 请求中,消息由浏览器编码 进入 URL,因此更容易被篡改。

    请注意,这种执行 GET 请求的方法确实带有一些 安全风险。根据MSDN documentation for UseHttpGet:

    希望对你有帮助

    【讨论】:

    • frebin,查看我的编辑,我已经给出了 URL,如何使用带参数的 URL 调用它。谢谢
    • 添加命名空间System.Web.Script.Services获取[ScriptMethod] Assembly:System.Web.Extensions.dll
    • frebin,如何通过调用 URL 使用代码获取其返回值。
    • frebin,使用 WPF 和本地 Web 服务
    • 这个来自 youtube 的视频展示了你想要的一切youtube.com/watch?v=TDOJJ0sd_bw
    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 2017-05-14
    • 2014-08-02
    • 1970-01-01
    • 2016-08-19
    • 2015-03-07
    • 2013-03-27
    • 2017-05-27
    相关资源
    最近更新 更多