ASP.Net Core更改默认视图文件夹位置 [英] ASP.Net Core Change Default Views Folder Location

查看:544
本文介绍了ASP.Net Core更改默认视图文件夹位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改默认视图位置,以便进行以下操作:

I'm trying to change the default views location so the following works:

[Route("")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

我想要查看的位置是/ MVC / Views / ControllerName / Index(MethodName)

The location of where I want the views is /MVC/Views/ControllerName/Index(MethodName)

我尝试通过将以下内容添加到Startup => ConfigureServices(IServiceCollection)

I've attempt by adding the following to Startup => ConfigureServices (IServiceCollection)

services.Configure<RazorViewEngineOptions>(o =>
{
    o.AreaViewLocationFormats.Clear();
    o.AreaViewLocationFormats.Add("/MVC/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
});

以下方法有效,但我希望它默认使用正确的路径

The following works but I would prefer for it to default to the correct path

return View("/MVC/Views/Home/Index.cshtml");


推荐答案

答案:

在.Net-Core v2.0或更高版本中,您可以使用<$ c $在RazorViewEngineOptions中使用c> ViewLocationFormats 和 AreaViewLocationFormats 来修改视图查找。

From .Net-Core v2.0 upwards you can use ViewLocationFormats and AreaViewLocationFormats in RazorViewEngineOptions to modify the View look-up.

您正在寻找的选项是 ViewLocationFormats ,因为您没有使用查看区域。

The option you are looking for is ViewLocationFormats since you're not using View Areas.

您的解决方案将不断发展这些行:

Your Solution would be along these lines:

services.Configure<RazorViewEngineOptions>(o =>
    {
        o.ViewLocationFormats.Clear();
        o.ViewLocationFormats.Add("/MVC/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
        o.ViewLocationFormats.Add("/MVC/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
    });

仅当您在该位置而不是在标准文件夹。

The last line is only needed if you have the shared Layouts and Paritals at that location and not in the Standard Folder.

这篇关于ASP.Net Core更改默认视图文件夹位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆