我知道有一个.NET库可以使用XML-RPC - 但有没有人知道它是否可以在ASP.NET MVC环境中使用?
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客户端指向服务并开始进行调用.