C
C#2y ago
populus

❔ View cannot be found.

LoginModel.cs Pages/Account/Login.cshtml Pages/Account/Register.cshtml AccountController.cs attached. Why am I getting the following error when submitting a POST request?
An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Register' was not found. The following locations were searched:
/Views/Account/Register.cshtml
/Views/Shared/Register.cshtml
/Pages/Shared/Register.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)
An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Register' was not found. The following locations were searched:
/Views/Account/Register.cshtml
/Views/Shared/Register.cshtml
/Pages/Shared/Register.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)
11 Replies
Angius
Angius2y ago
Why do you have your views in /Pages/ instead of /Views/ directory...? The exception is quite clear, it tells you what directories it searches through to find the appropriate view
populus
populusOP2y ago
Hey, so /Pages/ was the default folder added by the scaffolding. I tried creating a /Views/ folder and moving the relevant files there but I ran into some 405 issues so I assumed /Pages/ was the correct way. I can't quite puzzle it together.
Angius
Angius2y ago
If it was scaffolded code, that means what got scaffolded were some Razor Pages So either: a) Use them as they are b) Use controllers and views Trying to mix the two won't work
populus
populusOP2y ago
So /Pages/ is not a synonym for /Views/ then? Because that's what I got when creating the project ("scaffolding"(?)). For example, there is a "/Pages/Shared/" which was searched, and even though I tried putting the "Login.cshtml" and "Register.cshtml" there it did not find them.
Angius
Angius2y ago
The MVC paradigm uses /Controllers/ for controllers and /Views/ for views. The Razor Pages paradigm uses /Pages/ for... pages If you want to use MVC, it's best to place your views where views go, and controllers where controllers go If you want to use Razor Pages, place them where they go
populus
populusOP2y ago
I did not realize these were different frameworks. But it seems like using controllers and .cshtml files with the "@page" directive works? Because that's how I got it to work partially during my work on this. You would recommend me to troubleshoot this on google by using "MVC" as part of the keyword(s) searched?
Angius
Angius2y ago
I would recommend starting with placing things where they should go Or using the scaffolded Razor Pages code for auth, and not making your own implementation of it with MVC That would be the easiest
populus
populusOP2y ago
I am on the final stretches of my C# programming course and I guess MVC was what we learned. I don't know how I got Razor pages in the mix, but I think MVC is the way to go for me. But let's suppose I did want to do the Razor Pages code way, isn't it essentially just moving the code in there? I am using Aspnet.core.Identity and entityframeworkcore for Auth and DB respectively.
Angius
Angius2y ago
Identity will scaffold Razor Pages with login, registration, etc for you Once scaffolded, that's that, you don't need to touch them Or you can edit the codebehind in them if you want But trying to replace them with controllers, while still using the pages' .cshtml files is... an odd choice
populus
populusOP2y ago
Oh really, I must have navigated around that somehow. I installed it through the NuGet package manager and no code was provided by default.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server