Я пытаюсь получить доступ к URL: https: // localhost: 7252/assetcategory/index в приложении ASP.NET MVC. Я зарегистрировал пользователя через почтальон. У меня есть следующий код в моем assetCategoryController.cs :
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));
}
}
}
< /code>
Но мне показана ошибка: Эта страница сейчас не работает. Если проблема продолжается, свяжитесь с владельцем сайта. Http error 401
У меня есть аналогичный код в моих других контроллерах, и я использую аутентификацию JWT в ado.net.
Это скриншот в почтальнице:
И это скриншот задачи:
Подробнее здесь: https://stackoverflow.com/questions/795 ... -owner-htt
Эта страница сейчас не работает. Если проблема продолжается, свяжитесь с владельцем сайта. Ошибка HTTP 401 ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Джанго — произошла ошибка сервера. Пожалуйста, свяжитесь с администратором
Anonymous » » в форуме Python - 0 Ответы
- 30 Просмотры
-
Последнее сообщение Anonymous
-