Home > Back-end >  How can I access a folder directory from my controller with .NET 6?
How can I access a folder directory from my controller with .NET 6?

Time:09-21

I am trying to access my DataAccess folder in my HomeController class, but for some reason i am getting the error "Cannot resolve symbol 'DataAccess'". Can anyone help me figure out why? I have included my project structure below.

HomeController.cs:

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using ItunesMVC.Models;

namespace ItunesMVC.Controllers;

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public ActionResult Index()
    {
            
        ViewBag.SearchCounts = new DataAccess.ItunesSearchDBEntities().SearchCounts.OrderByDescending(a => a.Count).Take(25).ToList();
        return View();
    }
    
    public IActionResult Privacy()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Search(string Term)
    {
        try
        {
            var result = API.ItunesSearchAPI.Search(Term);
    
            ViewBag.value = Term;
            return View(result);
        }
        catch (Exception)
        {
    
            throw;
        }
    }
    
    [HttpGet]
    public ActionResult CountInc(string URL, string SearchTerm)
    {
        DataAccess.ItunesSearchDBEntities db = new DataAccess.ItunesSearchDBEntities();
    
        //Finding the term in database.
        var _term = db.SearchCounts.Where(a => a.Term == SearchTerm.ToLower()).FirstOrDefault();
        if (_term != null)
        {
            //If term is present Count is added
            _term.Count  ;
            db.Entry(_term).State = System.Data.EntityState.Modified;
        }
        else
        {
            //Term is saved in database
            db.SearchCounts.Add(new DataAccess.SearchCount() { Term = SearchTerm.ToLower(), Count = 1 });
        }
        db.SaveChanges();
        if (URL == null || URL == "")
            return RedirectToAction("NoURL");
    
        return Redirect(URL);
    }
    
    public ActionResult NoURL()
    {
        return View();
    }
}

enter image description here

CodePudding user response:

DataAccess is not a class, but you tring to create new Folder.MethodOfSomeClass()

You need to add Class here: new ClassName.MethodOfClassName()

After that Visual studio suggest you add using like:

using DataAccess.ClassName

CodePudding user response:

check the namespace in SearchCount.cs

  • Related