asp.Netでのweb api使用(mvcプログラム以外)
19213 ワード
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 WebApi
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// (Areas)
// dll,Microsoft Help Page , , (Areas)
AreaRegistration.RegisterAllAreas();
// api mvc 。
//WebApiConfig.Register(GlobalConfiguration.Configuration);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
// api json,
// , AllRoute() , , String , 123, json "123";
// json
//GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
// , json, AllRouteJson()
// ,
GlobalConfiguration.Configure(WebApiConfig.Register2);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace WebApi
{
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//
//config.MapHttpAttributeRoutes();
//
config.Routes.MapHttpRoute(
name: "DefaultApi", //
routeTemplate: "api/{controller}/{action}/{id}",//web api
defaults: new { id = RouteParameter.Optional }
);
}
//
public static void Register2(HttpConfiguration config)
{
//
config.MapHttpAttributeRoutes();
//
config.Routes.MapHttpRoute(
name: "DefaultApi", //
routeTemplate: "api/{controller}/{action}/{id}",//web api
defaults: new { id = RouteParameter.Optional }
);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
namespace WebApi
{
///
///
///
[RoutePrefix("api1")] // , route , route
public class DomeController: ApiController
{
// dll,Microsoft Help Page , , (Areas)
//
// : - - xml √, HelpPageConfig
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/WebApi.xml")));
// , xml
// , ///
//
// , ///
///
/// GetId
///
/// i
/// n
///
public DemoModel GetIdAnnotation(int i,int n)
{
return new DemoModel {id=1,name="a" };
}
//
// action, , ,
// ( id) , http://localhost:18444/api/Dome/GetIdRoute/1
// http://localhost:18444/api/Dome/GetIdRoute?i=1
///
/// GetIdRoute
///
///
///
public string GetIdRoute(int id)
{
return "GetIdRoute";
}
///
/// getAllRoute
///
///
// Get , [HttpGet] ,
[HttpGet]
public HttpResponseMessage AllRoute()
{
//
String userName = "AllRoute";
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(userName, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
///
/// ,
///
///
[HttpGet]
public HttpResponseMessage AllRouteJson()
{
// json
List dm = new List() { new DemoModel {id=1,name="name1",set=1},new DemoModel { id=2,name="name2",set=2} };
JavaScriptSerializer serializer = new JavaScriptSerializer();
String str = serializer.Serialize(dm);
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
// Get , :http://localhost:5123/api/Demo/GetSayHello?i=1&s=2
///
/// GetSayHello
///
///
/// i
/// n
///
public string GetSayHello(int i,string s)
{
return "hello world GetSayHello";
}
//{ : }" 。
///
/// 1
///
///
[Route("Route/RouteActionName/{name:int?}")]//http://localhost:18444/api1/Route/RouteActionName/1?customerId=2 int? ,int
[HttpGet]
public string RouteAction(int customerId)
{
return "hello world GetSayHello";
}
///
/// 2
///
///
[Route("Route/{text}/RouteActionName/{text2}")]//http://localhost:18444/Route/1/RouteActionName?customerId=1
[HttpGet]
public string RouteAction2(int customerId)
{
return "hello world GetSayHello";
}
///
/// 3
///
/// text
/// id
/// i
///
[Route("Route/{text}/RouteActionName/{text2}/{id}")]//http://localhost:18444/Route/t/RouteActionName/2/h?i=2,url
[HttpGet]
public string RouteAction3(int i,string id)
{
return "RouteAction3";
}
}
}
// API 。
// Nuget WebApiTestClient。
// Areas\HelpPage\Views\Help\Api.cshtml :
// @Html.DisplayForModel("TestClientDialogs")
// @section Scripts
//{
//
// @Html.DisplayForModel("TestClientReferences")
// ,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApi
{
public class DemoModel
{
///
/// id
///
public int id { get; set; }
///
///
///
public string name { get; set; }
//
public int set { get; set; }
}
}