asp.net-mvc MVC - 无法解析视图(不同项目中的控制器和视图)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16941547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:54:31  来源:igfitidea点击:

MVC - Cannot resolve view (controllers and views in separate projects)

asp.net-mvcasp.net-mvc-4controller

提问by Mithrilhall

I have a Solution with 4 projects:

我有一个包含 4 个项目的解决方案:

  • AS.Core.Common(references: Data, Web)
  • AS.Core.Controllers(references: Common, Data, Web)
  • AS.Core.Data
  • AS.Core.Web(references: Data)
  • AS.Core.Common(参考资料:数据、网络)
  • AS.Core.Controllers(参考资料:Common、Data、Web)
  • AS.Core.Data
  • AS.Core.Web(参考资料:数据)

In my controllers, anywhere that I return a view:

在我的控制器中,我返回视图的任何地方:

return View("~/Views/Home/Index.cshtml");

return View("~/Views/Home/Index.cshtml");

It is highlighted in red and I get a "Cannot resolve view '~/Views/Home/Index.cshtml'".

它以红色突出显示,我得到一个"Cannot resolve view '~/Views/Home/Index.cshtml'".

All four projects build successfully but when I hit F5 I get the following:

所有四个项目都成功构建,但是当我点击 F5 时,我得到以下信息:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

“/”应用程序中的服务器错误。

无法找到该资源。

描述:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的网址:/

版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.17929

How can I get my controllers to see my views so they resolve properly? I'm assuming this is why I get the 404 response.

如何让我的控制器看到我的视图,以便它们正确解析?我假设这就是我收到 404 响应的原因。

My Global.asax.cslook like so:

我的Global.asax.cs样子是这样的:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Register the default hubs route: ~/signalr
        RouteTable.Routes.MapHubs();

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

My RouteConfig.cs(I added namespaces but it appears to have not helped)

我的RouteConfig.cs(我添加了命名空间,但似乎没有帮助)

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        namespaces: new[] { "AS.Core.Controllers" },
        defaults: new 
        {
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional
        });
}

This is my HomeController.cs

这是我的 HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //return View("~/Views/Home/Index.cshtml");
        return View();
    }
 }

回答by Felipe Oriani

In asp.net-mvcyou could return a valid View in the respective controller's views's folder. For sample, if you have a controller called Product, you could have a folder in the path ~/Views/Product/Index.cshtml. In your Indexaction, you just return a View using the View()method of the Controllerclass base that all controllers in asp.net mvc should inherits. For sample:

asp.net-mvc 中,您可以在相应控制器的视图文件夹中返回有效视图。例如,如果您有一个名为 Product 的控制器,则路径中可能有一个文件夹~/Views/Product/Index.cshtml。在您的Index操作中,您只需使用asp.net mvc 中的所有控制器都应继承View()Controller类库的方法返回一个视图。样品:

public ActionResult Index()
{
   return View();
}

Asp.Net will find a View on the folder with the same name of your Action, Indexin this case.

Index在这种情况下,Asp.Net 将在与您的操作名称相同的文件夹中找到一个视图。

You also could return another View using the Viewmethod since you have the respective View on the folder, for sample:

您还可以使用该View方法返回另一个视图,因为您在文件夹上有相应的视图,例如:

public ActionResult Index()
{
   return View("About");
}

Considering this case, you should have a View named Aboutin the Productfolder. For sample: ~/Views/Product/About.cshtml.

考虑到这种情况,您应该AboutProduct文件夹中命名一个视图。样品:~/Views/Product/About.cshtml.

If you change your controllers to another project (class library), you have to set on the asp.net mvc initialization what is the default namespace of your controllers, try something like this on the Global.asaxfile and Application_Startmethod:

如果将控制器更改为另一个项目(类库),则必须在 asp.net mvc 初始化时设置控制器的默认命名空间,在Global.asax文件和Application_Start方法上尝试如下操作:

ControllerBuilder.Current.DefaultNamespaces.Add("NamespaceOfYourProject.Controllers");

Take a look at this article: http://dotnetslackers.com/articles/aspnet/storing-asp-net-mvc-controllers-views-in-separate-assemblies.aspx

看看这篇文章:http: //dotnetslackers.com/articles/aspnet/storing-asp-net-mvc-controllers-views-in-separate-assemblies.aspx