问题描述
我创建了一个项目ASP.NET Core 3.0 MVC,它运行良好.在同一解决方案中,我添加了3个(或更多)项目"Razor Class Library".每个此类项目都有一个控制器.如何配置路由,以便可以访问这些项目的控制器方法?
I created a project ASP.NET Core 3.0 MVC and it works fine. In the same solution, I added 3 (or more) projects "Razor Class Library". Each such project has a controller. How do I configure routing so that I can access the controller methods of these projects?
我的解决方案如下:
MyProject.Shop (Razor Class Library)
|_ Controllers
|_ HomeController.cs
MyProject.Books (Razor Class Library)
|_ Controllers
|_ HomeController.cs
MyProject.Web (main ASP.NET Core 3.0 MVC project)
|_ Controllers
|_ HomeControlles.cs
当前,路由仅在一个主项目中完美运行.您无法在Myproject.Book等中访问控制器.也许您需要配置应用程序部件",但是在我的情况下我该怎么做?
Currently, routing works perfectly only within one main project. You can't access the controller in Myproject.Book etc. Maybe you need to configure Application Parts, but how do I do this in my case?
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapDefaultControllerRoute();
});
推荐答案
为使每个库都能使用,您需要做一些事情,所以让我们使用您的 Shop
库为例,这是针对ASP.NET Core 3.1的.
There are a few things you need to do in order to make this work for each library, so let's go over each one, using your Shop
library as an example, and this is for ASP.NET Core 3.1.
右键单击您的 Web
项目的 Dependencies
文件夹,选择 Add Project Reference ...
,选中旁边的框>商店
,然后点击确定
.
Right-click the Dependencies
folder of your Web
project, select Add Project Reference...
, check the box next to Shop
and click OK
.
对于您而言,这意味着您的控制器和视图应位于 Areas/Shop
文件夹下,如下所示:
In your case, this means your controllers and views should be under an Areas/Shop
folder, like this:
对于您的 Shop
项目,这意味着对 Shop
项目的 HomeController
类执行以下操作:
For your Shop
project, this means doing the following for your Shop
project's HomeController
class:
[Area("shop")]
public class HomeController : Controller
将 _ViewStart.cshtml
复制到您的 Shop
项目
从您的 Web
项目中,将 Views/_ViewStart.cshtml
复制到您的 Shop
项目的 Areas/Shop/Views
文件夹.看起来应该像这样:
Copy _ViewStart.cshtml
to your Shop
project
From your Web
project, copy Views/_ViewStart.cshtml
to your Shop
project's Areas/Shop/Views
folder. It should look like this:
因此,您的 Web
和 Shop
项目之间的布局是一致的.
This is so your layout is consistent between your Web
and Shop
projects.
在此示例中,路由在您的 Web
项目的 Startup.cs
中进行配置,如下所示:
In this example, the routes are configured in your Web
project's Startup.cs
like so:
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "shop-default",
areaName: "shop",
pattern: "shop/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
现在,启动您的应用程序并转到默认URL应该会显示正常的主页,但是导航到/shop
将显示您在 Views/Shop/Index中的所有内容.cshtml
视图.
Now, launching your application and going to the default URL should show you the normal home page, but navigating to /shop
will show you whatever you have in your Views/Shop/Index.cshtml
view.
通过上述操作,我看到了主页:
From doing the above, I see the home page:
,并且还可以导航至商店:
and can also navigate to the shop:
...而且我显然饿了,想要冰淇淋.
...and I'm apparently hungry and wanting icecream.
自然,您需要对 Books
库执行相同的过程.
Naturally, you'll need to go through the same kind of process for your Books
library.
这篇关于ASP.NET Core Web应用程序与Razor类库项目之间的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!