Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im working in a WebApi in ASP.NET Core , but then when I try the next code, return View says it doesnt exists.

C#
[HttpGet]
        [AllowAnonymous]
        public IActionResult ResetPassword(string recToken, string email)
        {
            // If password reset token or email is null, most likely the
            // user tried to tamper the password reset link
            if (recToken == null || email == null)
            {
                ModelState.AddModelError("", "Invalid password reset token");
            }
            //            return View();
            return Ok("Vista ResetPassword"); // <-- When I put return View it says "View does not exists in current context"
        }


TBH its my first time working with it, but in Google there are some projects where the Api got Views working, but for some reason, when I use a View it throws that error and the correction options are also marked as not exists. Why is happening that or there is some way to redirect??

What I have tried:

I ve tried to put the View which Im referencing to the project, but the line return View its still marked as not exists.
Posted
Updated 12-Dec-19 6:19am
v2
Comments
ZurdoDev 10-Dec-19 13:48pm    
If you do not tell it what view then it will look for a view with the same name as your method, in this case ResetPassword, I believe.
GehoMP 10-Dec-19 14:11pm    
I ve tried to put the View, in this case Reset Password and nothing, its still marking "View does not exists".
ZurdoDev 10-Dec-19 14:17pm    
Then you likely have it misspelled. For example, it won't be like you have it in the code above, "Vista ResetPassword."
F-ES Sitecore 11-Dec-19 4:14am    
View will only be available if your class inherits from "Controller". That's for standard .net MVC, I don't know about WebApi core, but I doubt a WebApi controller is going to have inbuilt support for Views as they don't tend to return HTML.
GehoMP 12-Dec-19 12:12pm    
... dude you were right. My controller got ControllerBase, then I just left the word Controller and all the errors were chased away, then I ran the api and it ran the views with no problem. Thanks. :D

1 solution

As F-ES Sitecore said, the controller needs to inherit from Controller. By default WebApi Core inherits from ControllerBase. Changing it from ControllerBase to Controller makes the return View valid.

C#
public class CuentasController : Controller//Base
    {
        [HttpGet]
        [AllowAnonymous]
        public IActionResult ResetPassword(string recToken, string email)
        {
            // If password reset token or email is null, most likely the
            // user tried to tamper the password reset link
            if (recToken == null || email == null)
            {
                ModelState.AddModelError("", "Invalid password reset token");
            }
                        return View();
        }
        }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900