I got this error when creating ASP.NET Core 8 MVC application:
InvalidOperationException: The view ‘Edit’ was not found. The following locations were searched:
/Views/Category/Edit.cshtml
/Views/Shared/Edit.cshtml
To solve this, I added:
<code>builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
</code>
<code>builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
</code>
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
I never use this service in the past projects when creating scaffolding.
Controllers/CategoryController.cs
:
<code>// GET: Category/Edit/5
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
Category? category = _db.Categories.Find(id);
if (category == null)
{
return NotFound();
}
return View(category);
}
</code>
<code>// GET: Category/Edit/5
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
Category? category = _db.Categories.Find(id);
if (category == null)
{
return NotFound();
}
return View(category);
}
</code>
// GET: Category/Edit/5
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
Category? category = _db.Categories.Find(id);
if (category == null)
{
return NotFound();
}
return View(category);
}
Views/Category/Edit.cshtml
:
<code>@model Category
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
</code>
<code>@model Category
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
</code>
@model Category
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
Can any experts help explain? Is there a way to avoid using the service mentioned above?
Thanks.