在 mvc 中使用 IViewLocationExpander

Working with IViewLocationExpander in mvc(在 mvc 中使用 IViewLocationExpander)
本文介绍了在 mvc 中使用 IViewLocationExpander的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从自定义位置呈现视图,因此我实现了IViewLocationExpander 类中的接口.我在 Startup 类中注册了相同的类,如下所示.

I want to render views from a custom location, so for that I have implemented the IViewLocationExpander interface in a class. I have registered the same class in Startup class as follows.

启动

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

CustomViewLocationExpander

CustomViewLocationExpander Class

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)
    {

    }
}

最后,我的应用程序的视图组织如下:

And, finally, my application’s views are organized as follows:

我的问题:如果我通过以下 URL 从 ViewsFrontend 文件夹访问 Views/Login 视图:

My issue: If I access the Views/Login view from the ViewsFrontend folder from the following URL:

http://localhost:56739/trainee/Login/myclientname 

但随后立即将浏览器中的网址更改为:

But then immediately change the URL in the browser to:

http://localhost:56739/admin/Login/myclientname

在这种情况下,它仍然引用 ViewsFrontend 文件夹,尽管它现在应该引用 ViewsBackend 文件夹,因为 URL 开头trainee 应指 ViewsFrontend 文件夹,而以 admin 开头的应指 ViewsBackend 文件夹.

In this case, it still refers to the ViewsFrontend folder, even though it should now refer to the ViewsBackend folder, since URLs beginning with trainee should refer to the ViewsFrontend folder, while those beginning with admin should refer to the ViewsBackend folder.

另外,浏览器中修改URL后,只调用PopulateValues()方法,不调用ExpandViewLocations()方法.

Further, after changing the URL in the browser, it only calls the PopulateValues() method, but not the ExpandViewLocations() method.

如何重新配置​​此类以适用于其他文件夹?

How can I reconfigure this class to work for this other folder?

推荐答案

PopulateValues 作为一种方式来指定您的视图查找将根据每个请求而变化的参数.由于您没有填充它,视图引擎使用来自早期请求的缓存值.

PopulateValues exists as a way to specify parameters that your view lookup would vary by on a per-request basis. Since you're not populating it, the view engine uses cached values from an earlier request.

要解决这个问题,请将您的 ApplicationType 变量添加到 PopulateValues() 方法中,并且应该在该值时调用 ExpandValues() 方法变化:

To solve this, add your ApplicationType variable to the PopulateValues() method and the ExpandValues() method should get called whenever that value changes:

public class CustomViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        string folderName = context.Values["ApplicationType"];
        viewLocations = viewLocations.Select(f => f.Replace("/Views/", "/" + folderName + "/"));

        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {
        var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService<SessionServices>();
        string applicationType = session.GetSession<string>("ApplicationType");
        context.Values["ApplicationType"] = applicationType;
    }
}

这篇关于在 mvc 中使用 IViewLocationExpander的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)