开发者

XML-RPC in ASP.NET MVC

I know there is a library for .NET to use XML-RPC - but does anyone know if it can be used in the ASP.NET MVC enviro开发者_如何学JAVAnment or not?


XML-RPC.NET can be used with ASP.NET MVC.

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.


The Cook Computing xml-rpc.net library can be used with any ASP.NET project, including ASP.NET MVC.

http://xml-rpc.net/


The above answer is correct - it will work (you can simply deploy an XML RPC endpoint in your MVC app and call it directly).

If, however, what you mean is does it work with MVCs routing and controller actions, then I'm afraid the answer is 'not without a lot of work'. The well known XML RPC library (by cook computing is it?) is based on something similar to asmx technology.

There is also a WCF XML RPC component (faster and more future proof I think) - which I've written about in the context of writing a Live Writer backend. But this also will not integrate directly with MVC routing and Controller Actions.

Whilst it's possible that you could theoretically write ActionFilters, a custom Controller Factory, ModelBinder and (probably) more to provide XML Rpc results etc; you're better off simply choosing your XML RPC implementation and calling it directly.

Not sure if this has added anything useful :)


If it works with .NET, it works with ASP.NET MVC. The ASP.NET MVC environment is just a layer on top of the .NET Framework, so anything you can build with .NET can be used in an ASP.NET MVC application.


I was trying to find a solution for integrating XML-RPC with MVC, but couldn't find anyone who had done it, so I did it myself by implementing a custom route, a custom action filter (to convert the xml-rpc parameters to the action parameters) and custom action result (to convert the response back into a valid xml-rpc response). Read more here:

http://tech-journals.com/jonow/2012/01/25/implementing-xml-rpc-services-with-asp-net-mvc


I want to add a project called "XmlRpcMvc" I started back in 2011. You can find the source code on GitHub. Feel free to fork or do whatever you want ;)

It's also available via NuGet: Install-Package XmlRpcMvc (MetaWeblog sample)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜