You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.4 KiB

2 years ago
using AspNetCoreMVC.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace AspNetCoreMVC.Controllers
{
2 years ago
//[Route("Home")]
[Route("[controller]")] // 컨트롤러 이름 자동으로 따라감
2 years ago
public class HomeController : Controller
{
private IEnumerable<IBaseLogger> _baseLoggers;
2 years ago
public HomeController(IEnumerable<IBaseLogger> loggers)
2 years ago
{
_baseLoggers = loggers;
2 years ago
}
//private readonly ILogger<HomeController> _logger;
//public HomeController(ILogger<HomeController> logger)
//{
// _logger = logger;
//}
[Route("Index")]
[Route("/")]
2 years ago
public IActionResult Index()
{
2 years ago
//string url = Url.Action("Privacy", "Home");
//string url = Url.RouteUrl("test", new { test = 123 });
//return Redirect(url);
2 years ago
//return RedirectToAction("Privacy");
2 years ago
//return View();
//FileLogger logger = new FileLogger(new FileLogSetting("log.txt"));
//logger.Log("Log Test");
foreach (var logger in _baseLoggers)
{
logger.Log("DI Log Test");
}
return Ok();
}
[Route("DITest")]
public IActionResult ActionDITest([FromServices] IBaseLogger logger)
{
logger.Log("Log Test");
return Ok();
2 years ago
}
2 years ago
public IActionResult Test()
{
TestViewModel viewModel = new TestViewModel()
{
Names = new List<string>()
{
"One", "Two", "Three"
}
};
return View(viewModel);
}
2 years ago
[HttpPost("Post")]
public IActionResult PostOnly()
{
return Ok(1);
}
[Route("Test")] // 컨트롤러 하위 상대경로
[Route("/TestSecret")] // 절대경로
public IEnumerable<string> ApiTest()
{
List<string> names = new List<string>()
{
"Four", "Five", "Six", "Seven"
};
return names;
}
public IActionResult Sample()
{
SampleViewModel viewModel = new SampleViewModel()
{
Id = 1005,
Count = 2
};
return View(viewModel);
}
public IActionResult BuyItem(int id, int count)
{
return View();
}
2 years ago
public IActionResult Test1(int id, [FromHeader] string value)
2 years ago
{
return null;
}
public IActionResult Test2(TestModel testModel)
{
if (!ModelState.IsValid)
return RedirectToAction("Error");
return null;
}
//1) names[0]=Faker&names[1]=Deft
//2) [0]=Faker&[1]=Deft
//3) names=Faker&names=Deft
public IActionResult Test3(List<string> names)
{
return null;
2 years ago
}
public IActionResult Privacy()
{
2 years ago
//int a = 1;
//int b = 3 / (a - 1);
2 years ago
ViewData["Message"] = "Data from privacy";
2 years ago
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}