当前位置:  开发笔记 > 编程语言 > 正文

ASP.NET MVC中的XML-RPC

如何解决《ASP.NETMVC中的XML-RPC》经验,为你挑选了1个好方法。

我知道有一个.NET库可以使用XML-RPC - 但有没有人知道它是否可以在ASP.NET MVC环境中使用?



1> 小智..:

XML-RPC.NET可以与ASP.NET MVC一起使用.

为XML-RPC服务定义接口,例如:

using CookComputing.XmlRpc;

public interface IStateName
{
  [XmlRpcMethod("examples.getStateName")]
  string GetStateName(int stateNumber);
}

实施服务:

using CookComputing.XmlRpc;

public class StateNameService : XmlRpcService, IStateName
{
  public string GetStateName(int stateNumber)
  {
    if (stateNumber < 1 || stateNumber > m_stateNames.Length)
      throw new XmlRpcFaultException(1, "Invalid state number");
    return m_stateNames[stateNumber - 1];
  }

  string[] m_stateNames
    = { "Alabama", "Alaska", "Arizona", "Arkansas",
        "California", "Colorado", "Connecticut", "Delaware", "Florida",
        "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", 
        "Kansas", "Kentucky", "Lousiana", "Maine", "Maryland", "Massachusetts",
        "Michigan", "Minnesota", "Mississipi", "Missouri", "Montana",
        "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", 
        "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
        "Oregon", "Pennsylviania", "Rhose Island", "South Carolina", 
        "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", 
        "Washington", "West Virginia", "Wisconsin", "Wyoming" };
}

实现自定义路由处理程序:

using System.Web;
using System.Web.Routing;

public class StateNameRouteHandler : IRouteHandler
{
  public IHttpHandler GetHttpHandler(RequestContext requestContext)
  {
    return new StateNameService();
  }
}

在global.asax.cs中注册自定义路由:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.Add(new Route("api/statename", new StateNameRouteHandler()));

  // ...

}

通过将浏览器指向处理程序的URL来检查一切是否正常,例如在从Visual Studio运行时,http:// localhost:33821/api/statename.然后,您应该看到该服务的自动生成的帮助页面.如果可以,那么将XML-RPC客户端指向服务并开始进行调用.

推荐阅读
爱唱歌的郭少文_
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有