Asp.net中常用的数据交互是WebApi的方式,对于请求者只是向一个url发起请求。对于SaltUI,官方推荐使用salt-fetch.js来进行数据交互,当然直接使用zepto.js或者jquery.js以ajax来交互也是可以的。
接上一篇《钉钉开发系列(九)SaltUI在VS中的开发》,我们在项目SaltUIDemo中构造WebApi端,在项目中添加一个Global.asax和WebApiConfig.cs,具体代码如下
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.SessionState;
namespace SaltUIDemo
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
需要添加System.Web.Http、System.Web.Http.WebHost、System.Web.Mvc的引用,我们采用4.0版本的。
WebApiConfig.cs
using System.Web.Http;
namespace SaltUIDemo
{
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//注册路由映射
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",//action表示按方法路由
defaults: new { id = RouteParameter.Optional }
);
}
}
}
在项目中添加Controllers文件夹,并在其下增加SystemController.cs的文件,代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace SaltUIDemo.Cont