Screenshot в журнале «Почтальон»: , когда я пытаюсь получить доступ к контроллере в Url: URL: URL: URL: URL: URL: URL: URL: URL: URL: URL: URL: URL: URL: URL: Url: URL: URL: URL: URL: Url: URL: URL: https: // localhost: 7252/assetcategory/index
мне показали следующую ошибку.
I Decoding token It -in post in IN IN. Показано имя пользователя и претензии, как показано.
Код: Выделить всё
using Internship.DAL.Repositories;
using Internship.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Internship.Controllers
{
[Authorize(Roles = "Admin")]
public class AssetCategoryController: Controller
{
private readonly AssetCategoryRepository _assetCategoryRepository;
// Constructor to initialize the repository
public AssetCategoryController()
{
_assetCategoryRepository = new AssetCategoryRepository(); //line 21 // You could also use Dependency Injection for better testing and maintainability
}
// Action to display all asset details
public IActionResult Index()
{
// Get all asset details from the repository
List assetCategories = _assetCategoryRepository.GetAllAssetCategories();
// Pass the asset details to the view
return View(assetCategories);
}
// Other actions like Create, Edit, Delete can be added here
// GET: AssetCategory/Create
public IActionResult Create()
{
return View();
}
// POST: AssetCategory/Create
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(AssetCategory assetCategory)
{
if (ModelState.IsValid)
{
_assetCategoryRepository.CreateAssetCategory(assetCategory);
return RedirectToAction(nameof(Index)); // Redirect to the list after creation
}
return View(assetCategory); // Return to the form with validation errors if any
}
public IActionResult Details(int CatID)
{
// Get the asset category by CatID from the repository
AssetCategory assetCategory = _assetCategoryRepository.GetAssetCategoryById(CatID);
if (assetCategory == null)
{
return NotFound($"No asset category found with CatID {CatID}");
}
return View(assetCategory);
}
// GET: AssetCategory/Delete/5
public IActionResult Delete(int CatID)
{
AssetCategory assetCategory = _assetCategoryRepository.GetAssetCategoryById(CatID);
if (assetCategory == null)
{
return NotFound($"No asset category found with CatID {CatID}");
}
return View(assetCategory); // Show confirmation view
}
// POST: AssetCategory/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(int CatID)
{
_assetCategoryRepository.DeleteAssetCategory(CatID);
return RedirectToAction(nameof(Index));
}
// GET: AssetCategory/Edit/5
public IActionResult Edit(int CatID)
{
var assetCategory = _assetCategoryRepository.GetAssetCategoryById(CatID);
if (assetCategory == null)
{
return NotFound($"No asset category found with CatID {CatID}");
}
return View(assetCategory); // Show form with current values
}
// POST: AssetCategory/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(AssetCategory assetCategory)
{
if (ModelState.IsValid)
{
_assetCategoryRepository.UpdateAssetCategory(assetCategory);
return RedirectToAction(nameof(Index));
}
return View(assetCategory);
}
// GET: AssetCategory/PatchName/5
public IActionResult PatchName(int CatID)
{
var assetCategory = _assetCategoryRepository.GetAssetCategoryById(CatID);
if (assetCategory == null)
{
return NotFound($"No asset category found with CatID {CatID}");
}
return View(assetCategory); // Only show/edit the name field
}
// POST: AssetCategory/PatchName/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PatchName(int CatID, string CatName)
{
if (string.IsNullOrWhiteSpace(CatName))
{
ModelState.AddModelError("CatName", "Category name is required.");
var assetCategory = _assetCategoryRepository.GetAssetCategoryById(CatID);
return View(assetCategory);
}
_assetCategoryRepository.PatchAssetCategoryName(CatID, CatName);
return RedirectToAction(nameof(Index));
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... site-owner