我要重复使用的页面和控制器具有页面/控制器,例如
Index
,_Layout
,以及其他与我的Web应用程序中现有应用程序名称相冲突的其他页面,并且该文件夹同样具有clash.
的文件夹和文件。我知道我可以使用
wwwroot
将这些资源添加到我现有的Web应用程序中,但是如何以独立的方式添加它们,以便我要带来的应用程序部分完全在自己的路线中。因此,现有的应用程序将继续在默认URL上运行,但引用的程序集将在SayAddApplicationPart
?上运行。
我尝试调用哪个有效,但它覆盖了我现有的路线,我找不到使用其他路线的选项。
因此,现有的应用程序将继续在默认URL上运行,但引用的组件将在Say /Legacy上运行?
对于外部组件(Razor类库)中的静态文件,消耗静态文件时,我们需要使用前缀
/legacy
AddApplicationPart
前缀。在ASP.NET Core MVC中,要在外部引用的组件中添加剃须单和控制器的路由前缀,例如,在访问汇编的页面时,将“/legacy/lecacy/”添加到请求URL中,您可以使用自定义路由约定。请参阅以下样本:
创建.NET 9 Razor Class Library(Legacyrcl),该库支持页面和视图。然后添加剃须刀页面,控制器和静态文件。
在rclstyle.css文件中,我只添加一个类以设置背景颜色:
_content/{PACKAGE ID}/
the在剃须刀页面和MVC视图中,我们可以添加使用以下代码:
创建ASP.NET 9 MVC应用程序,然后添加Legacyrcl项目参考。注册剃须刀页面和MVC服务后,请使用以下代码:
.rclcolor{
background-color:aquamarine;
}
运行应用程序,我们可以访问RCL Razor页面,控制器和静态文件:
add自定义剃刀页路线约定。使用以下代码创建一个LegacyCypagerOuteModeLovention类:
<link rel="stylesheet" href="~/_content/LegacyRCL/css/rclstyle.css" asp-append-version="true" />
<h2 class="rclcolor">Razor Product Index</h2>
创建自定义控制器路线大会:创建一个legacyControllerRouteconeconeconeconvention类:
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
...
app.UseRouting();
app.UseAuthorization();
app.MapStaticAssets();
//app.UseStaticFiles();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();
app.MapRazorPages().WithStaticAssets();
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
namespace WebApplication8.Services
{
public class LegacyPageRouteModelConvention : IPageRouteModelConvention
{
private readonly Assembly _externalAssembly;
public LegacyPageRouteModelConvention(Assembly externalAssembly)
{
_externalAssembly = externalAssembly;
}
public void Apply(PageRouteModel model)
{
// Get the PageModel type using reflection
var pageType = GetPageModelType(model);
// Check if the PageModel type belongs to the external assembly
if (pageType != null && pageType.Assembly == _externalAssembly)
{
// Customize the route for pages in the external assembly
foreach (var selector in model.Selectors)
{
selector.AttributeRouteModel.Template = $"legacy/{selector.AttributeRouteModel.Template}";
}
}
}
private Type GetPageModelType(PageRouteModel model)
{
// The PageModel type is typically named after the Razor Page file
// For example, for "/Pages/Index.cshtml", the PageModel type is typically "IndexModel"
var pageName = model.ViewEnginePath.Trim('/').Replace("/", ".");
var pageModelTypeName = $"{pageName}Model";
// Search for the PageModel type in the external assembly
return _externalAssembly.GetTypes()
.FirstOrDefault(t => t.Name == pageModelTypeName);
}
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
namespace WebApplication8.Services
{
public class LegacyControllerRouteConvention : IControllerModelConvention
{
private readonly Assembly _externalAssembly;
public LegacyControllerRouteConvention(Assembly externalAssembly)
{
_externalAssembly = externalAssembly;
}
public void Apply(ControllerModel controller)
{
if (controller.ControllerType.Assembly == _externalAssembly)
{
foreach (var selector in controller.Selectors)
{
var template = selector.AttributeRouteModel?.Template;
if (template == null)
{
selector.AttributeRouteModel = new AttributeRouteModel(
new RouteAttribute("legacy/[controller]/[action]"));
}
}
}
}
}
}