Skip to content

Instantly share code, notes, and snippets.

@sahgilbert
Created June 3, 2019 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahgilbert/e006d66c9f466180f3dc4e3a0ce91420 to your computer and use it in GitHub Desktop.
Save sahgilbert/e006d66c9f466180f3dc4e3a0ce91420 to your computer and use it in GitHub Desktop.
C# ASP.Net Core - Unified MVC & Web Api Controller
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace SimonGilbert.Blog.Controllers
{
public class UnifiedController : Controller
{
[HttpGet]
public IActionResult Index()
{
ViewBag.Title = "This is the 'Index' action MVC VIEW";
return View();
}
[HttpGet]
public IActionResult About()
{
return Ok("This is the 'About' action WEB API endpoint");
}
[HttpGet]
public string Test(string id)
{
return $"You called the 'Test/{id}' action WEB API endpoint";
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
public IActionResult Send([FromBody]SendDataModel model)
{
return Ok($"You posted {model.Data} to the 'Send' action WEB API endpoint");
}
}
public class SendDataModel
{
public string Data { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment