Implementing an XML-RPC Service with ASP.NET MVC

來源:互聯網
上載者:User

I received a couple of emails recently asking how to implement an XML-RPC service in an ASP.NET MVC application. In case anyone is interested this is how to do it

Define an interface for your XML-RPC service, for example:

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

Implement the service:

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" };} 

Implement a custom route handler:

using System.Web;using System.Web.Routing;public class StateNameRouteHandler : IRouteHandler{  public IHttpHandler GetHttpHandler(RequestContext requestContext)  {    return new StateNameService();  }} 

Register the custom route in global.asax.cs:

public static void RegisterRoutes(RouteCollection routes){  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  routes.Add(new Route("api/statename", new StateNameRouteHandler()));  // ...} 

Check that everything is working by pointing your browser to the url for the handler, for example something like http://localhost:33821/api/statename in this case when running from Visual Studio. You should then see an automatically generated help page for the service. If this is ok then point your XML-RPC client to the service and start making calls.

上一篇:Python筆記:元組,列表,映射(字典)|下一篇:支援Linux下的Live Writer支援

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.