Asp.net core 在.NetCore 2.2项目中使用视图和ViewData时出错--

Asp.net core 在.NetCore 2.2项目中使用视图和ViewData时出错--,asp.net-core,view,controller,asp.net-core-2.2,viewdata,Asp.net Core,View,Controller,Asp.net Core 2.2,Viewdata,一个简短的问题。尝试从控制器方法返回视图时,我遇到以下错误 当前上下文中不存在名称视图 请参阅此图片以了解我的项目结构-- 我的代码是 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using AutoMapper; using M

一个简短的问题。尝试从控制器方法返回视图时,我遇到以下错误

当前上下文中不存在名称视图

请参阅此图片以了解我的项目结构--

我的代码是

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using AutoMapper;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Authorization;
    using CoreSpeechService.App.Utility;

    public IActionResult ProductInfo(int p_id)
    {
        if(p_id>0)
        {
            var pEntity = pService.GetProductById(p_id);
            if(pEntity!=null)
            {
                ViewData["ProductId"] = pEntity.Id;
                return View(pEntity);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        else
        {
            return BadRequest("Could not fetch detailes related to this product at the moment. Please try later.");
        }
    }
人们会认为我已经在控制器中添加了所有必要的名称空间。显然不是。我有一种预感,这主要是由于我的项目结构[控制器和cshtml文件]。正确的脚手架问题?什么 应该怎么做

建议。谢谢

**值得一提的是,我的控制器是从ControllerBase继承的,而不是从controller继承的

当前上下文中不存在名称视图

这是因为您的控制器继承自
ControllerBase
。您可以检查以下源代码,
ControllerBase
默认不包含
View()
方法。
controller
继承自
ControllerBase
并添加视图支持:

//A base class for an MVC controller with view support.
public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
{
    protected Controller();
     
    // Summary:
    //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object that renders a view to the
    //     response.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
    [NonAction]
    public virtual ViewResult View();
    //
    // Summary:
    //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a viewName.
    //
    // Parameters:
    //   viewName:
    //     The name or path of the view that is rendered to the response.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
    [NonAction]
    public virtual ViewResult View(string viewName);
    //
    // Summary:
    //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a viewName
    //     and the model to be rendered by the view.
    //
    // Parameters:
    //   viewName:
    //     The name or path of the view that is rendered to the response.
    //
    //   model:
    //     The model that is rendered by the view.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
    [NonAction]
    public virtual ViewResult View(string viewName, object model);
    //
    // Summary:
    //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a model to
    //     be rendered by the view.
    //
    // Parameters:
    //   model:
    //     The model that is rendered by the view.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
    [NonAction]
    public virtual ViewResult View(object model);       
}
在这种情况下,只需将
ControllerBase
更改为
Controller

public class ProductController : Controller { }
此外,我看到您的结构包含Razor页面。当您使用
返回视图(模型)
时,它将使用模型数据呈现Razor视图。请确保您的Razor视图名为
ProductInfo.cshtml
,而不是Razor页面名为
ProductInfo.cshtml

Hi@Rena——是的,我已经这样做了,并且工作正常。还有一个问题是我的文件夹的命名法,因此影响了我的名称空间,引起了歧义