相关疑难解决方法(0)

C# razor 应用程序中是否可以有多个布局页面?

以前,我使用 MVC 创建了 C# 应用程序,并且可以指定要在其文件中的视图上使用的布局视图。这样视图文件包含:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

/*Rest of file*/
Run Code Online (Sandbox Code Playgroud)

最近我开始使用 Razor 页面创建应用程序。到目前为止,我只有一个布局页面。但是,我想对页面子集使用不同的布局页面,并且我只能看到如何为 Pages 文件夹中的所有页面指定单个布局页面。由于布局是在_ViewStart.cshtml文件中声明的。该文件的内容很简单:

@{
    Layout = "_Layout";
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以对某些页面使用一个布局文件,然后对其他页面使用不同的布局文件?

asp.net-core razor-pages

6
推荐指数
1
解决办法
1万
查看次数

在mvc中使用IViewLocationExpander

我想从自定义位置渲染视图,所以为此我IViewLocationExpander在类中实现了 接口.我已startup file按如下方式注册了同一课程.

Startup.cs文件

 public void ConfigureServices(IServiceCollection services)
    {
       .....
        //Render view from custom location.
        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
        });
        ....
    }
Run Code Online (Sandbox Code Playgroud)

CustomViewLocationExpander类

public class CustomViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {

        var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService<SessionServices>();
        string folderName = session.GetSession<string>("ApplicationType");

        viewLocations = viewLocations.Select(f => f.Replace("/Views/", "/" + folderName + "/"));


        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序视图结构如下

在此输入图像描述

我的问题是,如果我ViewsFrontend从URL http:// localhost:56739/trainee/Login/myclientname访问文件夹中 的Views/Login视图,并立即将浏览器中的url更改为http:// localhost:56739/admin/Login/myclientname,那么在这种情况下它仍然引用 …

c# asp.net-mvc asp.net-core-mvc asp.net-core-1.0

3
推荐指数
1
解决办法
3259
查看次数