using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
//https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing
namespace LogFileWeb
{
[RoutePrefix("api/books")]
public class LogController : ApiController
{
// GET api/<controller>
[Route("get1")]
public IEnumerable<string> Get1()
{
WriteLog("Hêlo");
WriteLog("World");
return new string[] { "value1", "value2" };
}
// GET get2?id=5
[Route("get2")]
public IHttpActionResult Get2(int id)
{
string val = "value";
return Ok(new { V = val });
}
[Route("get3")]
public IHttpActionResult Get3(int id)
{
string val = "value";
return Ok(val);
}
[Route("get4")]
public string Get4(int id)
{
string val = "value";
return val;
}
//get5/1/2
[Route("get5/{one}/{two}")]
public string Get5(int paramOne, int paramTwo)
{
return "The [Route] with multiple params worked";
}
//with type: get6/1/2
[Route("get6/{one:int}/{two}")]
public string Get6(int paramOne, int paramTwo)
{
return "The [Route] with multiple params worked";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
protected void WriteLog(string log)
{
try
{
string logFile = HttpContext.Current.Server.MapPath("~/api/log.txt");
using (StreamWriter writetext = new StreamWriter(logFile, true, Encoding.UTF8))
{
writetext.WriteLine(log);
}
}
catch (Exception e)
{
}
}
}
}
Không có nhận xét nào:
Đăng nhận xét