我在我的ASP.NET Core MVC应用程序中有一个Admin
区域,当我试图达到/Admin/Category/Add
时,它说No webpage was found的网址:https://localhost:7002/Category/Add?area=Admin
.
我的路由在program.cs
:
app.MapDefaultControllerRoute();
app.MapAreaControllerRoute(
name: "Admin",
areaName: "Admin",
pattern: "Admin/{controller=Home}/{action=Index}/{id?}"
);
我的看法:
@model List<Category>
@{
ViewData["Title"] = "Index";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<a asp-area="Admin" asp-controller="Category" asp-action="Update" class="btn mb-1 btn-primary">Add Category</a>
<div class="card-title">
<h4>Table Basic</h4>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Created By</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.CreatedBy</td>
<td>@item.IsDeleted</td>
<td>
<a asp-area="Admin" asp-controller="Category" asp-action="Update" asp-route-id="@item.Id">
<button type="button" class="btn mb-1 btn-warning">Update</button>
</a>
<a asp-area="Admin" asp-controller="Category" asp-action="Delete" asp-route-id="@item.Id">
<button type="button" class="btn mb-1 btn-danger">Delete</button>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
我的控制器:
namespace AnimeUI.Areas.Admin.Controllers
{
[Area("Admin")]
public class CategoryController : Controller
{
private readonly ICategoryService categoryService;
private readonly IValidator<Category> validator;
private readonly IMapper mapper;
public CategoryController(ICategoryService categoryService, IValidator<Category> validator, IMapper mapper)
{
this.categoryService = categoryService;
this.validator = validator;
this.mapper = mapper;
}
public async Task<IActionResult> Index()
{
var categories = await categoryService.GetAllCategoriesNonDeletedAsync();
return View(categories);
}
[HttpGet]
public IActionResult Add()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Add(AddCategoryDto addCategoryDto)
{
var category = mapper.Map<Category>(addCategoryDto);
var result = validator.Validate(category);
var exists = await categoryService.Exists(category);
if (exists)
result.Errors.Add(new ValidationFailure("Name", "This category name already exists"));
if (result.IsValid)
{
await categoryService.CreateCategoryAsync(addCategoryDto);
return RedirectToAction("Index", "Category", new { Area = "Admin" });
}
result.AddToModelState(this.ModelState);
return View(addCategoryDto);
}
}
}
我一直在寻找一整天,但不能找到一个有效的解决这个恼人的问题.出于某种原因,我的URL是这样生成的:
https://localhost:7002/Category/Add?area=Admin
而不是
https://localhost:7002/Admin/Category/Add
我试着改变我的端口,添加不同的路由语法.我目前正在使用Visual Studio 2022,我的项目是. NET 8.0
PS:我的问题只发生在我需要go Admin
页的时候.