简体   繁体   English

如何使asp.net核心应用程序在当前项目中查找视图文件?

[英]How to make asp.net core application to look for view files in the current project?

I have two projects in my solution. 我的解决方案中有两个项目。 One ( main ) of them is the application entry point. 其中一个( 主要 )是应用程序入口点。 The other one ( api ) handles one of the business processes, which require JSON communication in 99% cases. 另一个( api )处理其中一个业务流程,在99%的情况下需要JSON通信。 However, for 1% of cases I need full HTML response, and here is the problem. 但是,在1%的情况下,我需要完整的HTML响应,这就是问题所在。 I created a controller and a view, but it seems that application looks for the view in the main project. 我创建了一个控制器和一个视图,但似乎应用程序在项目中查找该视图。

I want it to work with the following file structure within the solution: 我希望它与解决方案中的以下文件结构一起使用:

/Main/ (and the content of main application) /API/Controllers/TestController.cs (contains definition of Action()) /API/Views/Test/Action.cshtml

But it seems I need to copy the view from /API/Views/Test/Action.cshtml to /Main/Views/Test/Action.cshtml or /Main/Views/Shared/Action.cshtml . 但是似乎我需要将视图从/API/Views/Test/Action.cshtml复制到/Main/Views/Test/Action.cshtml/Main/Views/Shared/Action.cshtml That splits internal logic of my application :( 那分裂了我的应用程序的内部逻辑:(

You can keep your views with your DLL. 您可以使用DLL保留视图。 Just ensure on build/deployment copy 'views' folder at root location. 只需确保在根目录下的构建/部署副本“ views”文件夹中即可。

You cannot (currently, at least) share views between ASP.NET Core projects. 您(目前至少)不能在ASP.NET Core项目之间共享视图。 In ASP.NET Core 2.1 (in preview), you will be able to share views by adding them to a class library that both projects can reference. ASP.NET Core 2.1 (预览版)中,您可以通过将视图添加到两个项目都可以引用的类库中来共享视图。

Razor UI in a class library 类库中的Razor UI

ASP.NET Core 2.1 will make it easier to build and include Razor based UI in a library and share it across multiple projects. ASP.NET Core 2.1将使构建和在库中包含基于Razor的UI以及在多个项目之间共享它变得更加容易。 A new Razor SDK will enable building Razor files into a class library project that can then be packaged into a NuGet package. 新的Razor SDK将支持将Razor文件构建到类库项目中,然后可以将其打包到NuGet包中。 Views and pages in libraries will automatically be discovered and can be overridden by the application. 库中的视图和页面将被自动发现,并且可以被应用程序覆盖。 By integrating Razor compilation into the build, the app startup time is also significantly faster, while still allowing for fast updates to your Razor views and pages at runtime as part of an iterative development workflow. 通过将Razor编译集成到内部版本中,应用程序的启动时间也大大缩短,同时仍允许在运行时快速更新Razor视图和页面,这是迭代开发工作流程的一部分。

For the time being, you'll just have to duplicate the views in each project and put it on your road map to factor them out once 2.1 is released and you can upgrade. 暂时,您只需要在每个项目中复制视图并将其放在路线图上就可以在2.1发布并可以升级时将它们排除在外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将位于应用程序部分 (DLL) 中的部分视图呈现到主 asp.net 核心 2.2 项目中 - How to render a partial view located in an application part (DLL) into main asp.net core 2.2 project ASP.NET 核心 web 应用 - 如何上传大文件 - ASP.NET Core web application - How to upload large files 无法使用 IHttpContextAccessor 访问 ASP.NET 核心项目中的当前用户 - Unable to access current user in ASP.NET Core project with IHttpContextAccessor 如何更改asp.net核心视图 - How to change asp.net core view ASP.NET内核如何上传文件? - How to upload files in ASP.NET Core? Asp.net 内核如何在视图中获取当前页面? - Asp.net core how can i get current page in View? ASP.NET 核心 MVC 身份 - 如何查看当前登录的用户? - ASP.NET Core MVC Identity - how to get current logged in user in view? Asp.NET 核心 web 应用程序在创建新项目时不显示 - Asp.NET Core web application not showing on creation of new project ASP.NET Core 3.1 应用程序中的第一个项目 - First project in an ASP.NET Core 3.1 application 在 Docker 中运行 ASP.NET Core 应用程序时丢失文件:如何在容器运行时显示文件? - Missing files when running ASP.NET Core application in Docker: How to display files when the container is running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM