android webapi 返回html 代码,分布式技术 webapi 路由追加html、aspx、shtml 适用于 对接 安卓、IOS(示例代码)...

首先是这样,在对接安卓和IOS或者是第三方调用的接口,我需要在服务端返回一个带.html/.aspx这样后缀的接口。

例子如下图:http://localhost:64131/api/UsersInfo/GetAllUsersInfo.html

606be23911c749be8bc59f134c649399.jpg

当然,我们用的肯定是webapi 来做这件事情。不需要建aspx/html/do/shtml...这样的文件,只需要配置一下路由就可以了。

首先要打开:WebApiConfig 这个配置文件。需要将

routeTemplate: "api/{controller}/{action}/{Id}",

修改成:

如果要想是aspx,那么就修改成

routeTemplate: "api/{controller}/{action}.aspx",

如果要想是html,那么就修改成

routeTemplate: "api/{controller}/{action}.html",

修改完成代码如下:

public static classWebApiConfig

{public static voidRegister(HttpConfiguration config)

{

config.Routes.MapHttpRoute(

name:"DefaultApi",

routeTemplate:"api/{controller}/{action}.html",

defaults:new { id =RouteParameter.Optional }

);//取消注释下面的代码行可对具有 IQueryable 或 IQueryable 返回类型的操作启用查询支持。//若要避免处理意外查询或恶意查询,请使用 QueryableAttribute 上的验证设置来验证传入查询。//有关详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=279712。//config.EnableQuerySupport();//若要在应用程序中禁用跟踪,请注释掉或删除以下代码行//有关详细信息,请参阅:http://www.asp.net/web-api

config.EnableSystemDiagnosticsTracing();//将webapi中的XmlFormatter 移除,默认就是以JsonFormatter作为其传输格式

config.Formatters.Remove(config.Formatters.XmlFormatter);

}

}

webapi Controller代码:

43e3c789887846c9a67d35dff8fd61b8.jpg

2235fedf0195421387fddbfab6fea8ca.jpg

namespaceWebApiApp.Controllers

{public classUsersInfoController : ApiController

{

UsersInfo[] usersInfos= newUsersInfo[]

{new UsersInfo{Id=1,Name="张三", Age=100, Email="abcd@126.com", Adress="北京"},new UsersInfo{Id=2,Name="李四", Age=222, Email="abcd@126.com", Adress="北京"},new UsersInfo{Id=3,Name="王五", Age=333, Email="abcd@126.com", Adress="北京"},new UsersInfo{Id=4,Name="侯六", Age=444, Email="abcd@126.com", Adress="北京"}

};///

api/Contact///

///

public IEnumerableGetListAll()

{returnusersInfos;

}///

///获取全部数据///

///

publicReturnJson GetAllUsersInfo()

{return new ReturnJson { data =usersInfos };

}///

///根据Id 获取数据///

///

///

public UsersInfo GetUsersInfoById(int Id,[FromBody]stringvalue )

{var usersInfo = usersInfos.FirstOrDefault((u) => u.Id ==Id);returnusersInfo;

}public classReturnJson

{public int count { get; set; }public object data { get; set; }public string msg { get; set; }

}

}

}

View Code

新建一个html页面,供测试使用:

所有用户

{{each data as list i}}Id:{{list.Id}} , Name:{{list.Name}},Age:{{list.Age}},Email: {{list.Email}},Adress:{{list.Adress}}

{{/each}}

查询

varurl_api= \'api/UsersInfo/\';

$(function() {

GetAllUsersInfo();

})functionGetAllUsersInfo() {varstrli= "";

$.ajax({

type:\'GET\',

url:\'api/UsersInfo/GetAllUsersInfo.html\',

async:true,//同步

dataType:\'json\',

success:function(data) {varjsondata=data.data;varlist_html=template(\'js:usersinfos\', data);

$(\'#usersinfos\').append(list_html);//$.each(json, function (i, n) {

//strli += "

";

//strli += \'Id:\' + n.Id + \',\' + \'Name:\' + n.Name + \',\' + \'Age:\' + n.Age + \',\' + \'Email:\' + n.Email + \',\' + \'Adress:\' + n.Adress + \'\';

//strli += \'

\';

//})

//$("#usersinfos").html(strli);

},

error:function(xhr, status, error) {

alert("操作失败");

}

});

}functionGetUsersInfoById() {varstrli_ById= "";varId=$(\'#Id\').val();

$.ajax({

type:\'GET\',

url:\'api/UsersInfo/GetUsersInfoById.html\',

async:true,//同步

dataType:\'json\',

data: {\'Id\': Id },

success:function(json) {

strli_ById+= "

";

strli_ById+= \'Id:\' +json.Id+ \',\' + \'Name:\' +json.Name+ \',\' + \'Age:\' +json.Age+ \',\' + \'Email:\' +json.Email+ \',\' + \'Adress:\' +json.Adress+ \'\';

strli_ById+= \'

\';

$("#usersinfo").html(strli_ById);

},

error:function(xhr, status, error) {

alert("操作失败");

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值