1
public class BlogController : ApiController
{
   public IActionResult Index()
   {
       var webClient = new WebClient();
       // parse the json file
       var json = webClient.DownloadString(@"full path to json file");
       var blogPosts = JsonConvert.DeserializeObject<BlogPosts>(json);
       return View(blogPosts);
   }
}

The name "View" does not exist in the current context any Ideas what could be?

I am trying to parse JSON file and display the data in view which I created manually as doesn't let me do it from the action method? I am sure is something simple but I am new in asp.net core. Thank you

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
beetle_juice
  • 51
  • 2
  • 11
  • 1
    Change ": ApiController" to ": Controller"? ApiController does not implement View, but the regular Controller does. Give this a skim? https://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc – classicSchmosby98 Jan 11 '20 at 16:54
  • When I changed it giving me Cannot implicitly convert type 'System.Web.Mvc.ViewResult – beetle_juice Jan 11 '20 at 17:00
  • Try changing the return type of your method to ActionResult instead of IActionResult. ViewResult is derived from ActionResult and can only return Views – classicSchmosby98 Jan 11 '20 at 17:04
  • What's the complete error message ? `System.Web.Mvc.ViewResult` isn't part of ASP.NET Core, it's part of the Full framework. I suspect you didn't start the tutorial from the [Get Started](https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.2) page. The ASP.NET Core project templates add the correct packages that provide both IActionResult and ActionResult . – Xueli Chen Jan 13 '20 at 03:01
  • this project is part of the exam I have been given to do. So I had to add controllers models and view in the project I have added models and controller. I have added the ActionResult Method that parses the simple JSON file into the index.cshtml.The I have just added an index.cshtml view and I have the error when I run the project 403.14 forbidden.I have enabled directory browsing as well and it is the same.I assume it is something from project architecture I am missing packages or route config maybe. – beetle_juice Jan 13 '20 at 13:50

2 Answers2

1

So you want to return a HTML page displaying a list of blog posts. Then:

  • your controller should inherit BlogController : Controller rather than ApiController. That latter class is used to return JSON or XML data but not HTML

  • at the root of your solution, create a file Views/Blog/Index.cshtml

Controllers in the MVC framework rely on "convention over configuration". Indeed the method you return View(blogPosts) will try to find the cshtml page I mentionned above.

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
  • I have already Views/Blog/Index.cshtml in vs I have no issues found, also when I run it HTTP Error 403.14 - Forbidden.Is it possible to be from the DownloadString(@"Copy full path from json file")? – beetle_juice Jan 11 '20 at 19:22
  • Run your application in Debug mode and see where the exception is thrown. Otherwise write your JSON data directly in `var json = "{ ... }"` and tell us exactly what the issue you see – Guillaume S. Jan 12 '20 at 10:33
  • I did try to debug it and it is still the same error, also changed variable and it is the same error is possible to be from route config file as when I run different app everything is working fine? – beetle_juice Jan 12 '20 at 16:53
  • Is ti possible needs to install any packages from nuget packages manager ? – beetle_juice Jan 12 '20 at 17:28
0

You should inherit from Controller, instead of ApiController

public class BlogController : Controller
 {
    public IActionResult Index()
     {
       var webClient = new WebClient();
       // parse the json file
       var json = webClient.DownloadString(@"full path to json file");
       var blogPosts = JsonConvert.DeserializeObject<BlogPosts>(json);
       return View(blogPosts);
     }
 }
Sydney_dev
  • 1,448
  • 2
  • 17
  • 24