Hello,
Can I ask for tutorial/article for building MVC web site with forum?
If you can show me more options, thanks.
Hello,
Can I ask for tutorial/article for building MVC web site with forum?
If you can show me more options, thanks.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Simran VermaRank #831 · 2.7K reputationPosted Mar 22, 2024, 5:55 AM EDT
First, you need to have .NET Core SDK installed on your machine. You can download it from the official .NET website if you haven't already: https://dotnet.microsoft.com/download
Once you have .NET Core SDK installed, follow these steps to create a new Web API project:
This command will create a new directory named
MyWebAPIcontaining the basic structure of a Web API project.cd MyWebAPIOpen the project in your preferred code editor. For example, if you're using Visual Studio Code:
Now, let's create a simple API for managing items.
In
Controllersdirectory, you'll findWeatherForecastController.cs. You can rename it toItemsController.csand modify it to create an Items API.Here's an example of
ItemsController.cs:using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; namespace MyWebAPI.Controllers { [ApiController] [Route("[controller]")] public class ItemsController : ControllerBase { private static List _items = new List(); [HttpGet] public IEnumerable Get() { return _items; } [HttpPost] public IActionResult Post([FromBody] string item) { _items.Add(item); return Ok(); } [HttpDelete("{item}")] public IActionResult Delete(string item) { if (_items.Contains(item)) { _items.Remove(item); return Ok(); } else { return NotFound(); } } } }In this controller:
[ApiController]attribute denotes that this controller will respond to HTTP API requests.[Route("[controller]")]specifies that the controller will respond to requests with a route that starts with/items.Get()method returns the list of items.Post()method adds a new item to the list.Delete()method removes an item from the list.Now, you can run your API by executing the following command in your terminal or command prompt:
Your API will be hosted on
http://localhost:5000. You can test the API using tools like Postman or curl. Here are some example requests:That's a basic example of how you can create a Web API using ASP.NET Core MVC. You can expand upon this example by adding more complex logic, database integration, authentication, and authorization as needed.