-
- Download Visual Studio 2017+
-
- Install Asp.net
-
- If you have issues with the installation it might be due to the fact that your NuGet settings are Offline.
- Next to use this there are 3 main components needed to work with.
In most cases I think starting with the model will be the most simple due to it being kinda of a core to the program you want to build. I.e. lets build a website to show jokes. I can then make a model that would contain properties about what I want for the site.
so my id would be later linked to a db that would have the id of the joke stored, the joke and the answer along with the user who posted it.
After making the class you can then make a controller to manage the data. ---> I would recommend using the automatic generation features to make all the code for you other wise self coding can take a bit and be annoying.
After generating the code you should have a Controller like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using JokesWebApp_MVC.Data;
using JokesWebApp_MVC.Models;
using Microsoft.AspNetCore.Authorization;
namespace JokesWebApp_MVC.Controllers
{
public class JokesController : Controller
{
private readonly ApplicationDbContext _context;
public JokesController(ApplicationDbContext context)
{
_context = context;
}
// GET: Jokes
public async Task<IActionResult> Index()
{
return View(await _context.Joke.ToListAsync());
}
// POST: Jokes/ShowSearchResults
public async Task<IActionResult> ShowSearchForm()
{
return View();
}
public async Task<IActionResult> ShowSearchResults(String SearchPhrase)
{
return View("Index",await _context.Joke.Where(j => j.JokeQuestion.Contains(SearchPhrase)).ToListAsync());
}
// GET: Jokes/Details/5
[Authorize]
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var joke = await _context.Joke
.FirstOrDefaultAsync(m => m.Id == id);
if (joke == null)
{
return NotFound();
}
return View(joke);
}
// GET: Jokes/Create
[Authorize]
public IActionResult Create()
{
return View();
}
// POST: Jokes/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,JokeQuestion,JokeAnswer,User")] Joke joke)
{
if (ModelState.IsValid)
{
_context.Add(joke);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(joke);
}
// GET: Jokes/Edit/5
[Authorize]
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var joke = await _context.Joke.FindAsync(id);
if (joke == null)
{
return NotFound();
}
return View(joke);
}
// POST: Jokes/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,JokeQuestion,JokeAnswer,User")] Joke joke)
{
if (id != joke.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(joke);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!JokeExists(joke.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(joke);
}
// GET: Jokes/Delete/5
[Authorize]
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var joke = await _context.Joke
.FirstOrDefaultAsync(m => m.Id == id);
if (joke == null)
{
return NotFound();
}
return View(joke);
}
// POST: Jokes/Delete/5
[Authorize]
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var joke = await _context.Joke.FindAsync(id);
_context.Joke.Remove(joke);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool JokeExists(int id)
{
return _context.Joke.Any(e => e.Id == id);
}
}
}
This will generate along side the Views which should create a Create, Delete, Details, Edit, and Index view and all should be linked up more or less.
Now if you run the page it should look somthing along the lines of this. I added some images so dont worry.
-
The only issue might occur, i cant remember if this happens b4 or after you click on the jokes page that i have but you can access it in the url via /jokes if you set up the site properly. --- You should get an error due to not linking your db with the site. That is an ez fix. In this case we will use migrations.
-
^^ this is cuz no one likes using DAO ( data access object) but we rather use ORM or (OBject relational Mapper) basically get vs to do the work for us rather than writing our own sql.
you are gonna want to type few lines in the package manager console.
enable-migrations
add-migration "What ever you wanna write here"
update-database
After this the website should be up and you can add or remove and play around with it anyway you want!
So as an example What we want is to inject the repo dependency into the item controller
So to register and construct the dependencies
How to register and inject dependencies in .NET 5
How to implement Data Transfer Objects (DTOs)
How to map entities to DTOs