Implementing an XML-RPC Service with ASP.NET MVC
2013-04-23 17:22
411 查看
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:
Implement the service:
Implement a custom route handler:
Register the custom route in global.asax.cs:
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支持
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支持
相关文章推荐
- asp.net mvc combine with reporting service
- 【翻译】ASP.NET MVC中实现一个错误处理过滤器[Implementing an Error-Handling Filter]
- ASP.NET Web Services系列(2) - What Is an XML Web Service?
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
- Writing an XML-RPC server or client in ASP.Net: Part 1
- ASP.NET MVC – Create easy REST API with JSON and XML(转)
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- Writing an XML-RPC server or client in ASP.Net: Part 2
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- 转载:Using JQuery with an ASP.NET webservice
- Security Measure To Prevent URL Parameter Forgery- An Example with ASP.Net MVC
- ASP.NET MVC Caching with OutputCache
- Twitter style paging with ASP.NET MVC and jQuery
- Sending e-mail with attachments from an ASP.NET page
- Implementing HTTPS Everywhere in ASP.Net MVC application.
- ASP.NET MVC 3 Performance – on par with MVC 2
- Intro to ASP.NET MVC 4 with Visual Studio [译三:添加一个控制器]
- Profile and debug your ASP.NET MVC app with Glimpse