在 ASP.NET 核心 MVC 中设置新的登陆页面

Setting a new landing page in ASP.NET Core MVC

我需要为我的应用程序设置一个新的登陆页面。目前,它位于 Views 文件夹内 Home 文件夹内的标准 Index.cshtml 上。我希望我的新登录页面来自这个目录:

Views/Welcome/Index.cshtml

我收到的错误是: InvalidOperationException:尚未为位于“/Views/Welcome/Index.cshtml”的页面调用 RenderBody。忽略调用 IgnoreBody();

到目前为止,我在 startup.cs 文件中进行了以下更改:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.Configure<RazorViewEngineOptions>(o =>
            {
                o.ViewLocationFormats.Clear();
                o.ViewLocationForms.Add("/Views/Welcome/Index" + RazorViewEngine.ViewExtension);
            });
        }


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Welcome}/{action=Index}/{id?}");
            });
            endpoints.MapFallbackToController("Index", "Welcome");
        }

我的看法:

@{
  ViewData["Title"] = "Index";
}

//html
//jQuery

我没有在网上找到任何关于如何在使用 MVC 时完成此操作的资源。

谢谢!

我想我找到了解决办法。我将 Layout = null 添加到我的视图(新的登录页面),但它删除了我所有的 css。然后,我将路径添加到此文件中的 css 库。它似乎有效,但我仍然愿意接受任何关于如何改进这些代码的建议(如果有的话)。谢谢!

这就是我找到解决方案的方式:Whosebug solution

Redirect to other view

如果您想 return 以简单的方式改变 view,您可以尝试重定向到 Home/Index 中的其他视图。

public class HomeController : Controller
{
        public IActionResult Index()
        {
            //return View();
            return View("~/Views/Welcome/Index.cshtml");
        }
 }

Layout

至于布局,_ViewStart.cshtml(/Pages/Shared) 运行 在每个完整视图(不是布局,也不是​​局部视图)之前。

_ViewStart.cshtml

@{
    Layout = "_Layout";
}

Render JS

在下面的代码中,您定义脚本部分并在该部分内呈现脚本。

Index.cshtml

 @RenderSection("Scripts", required: true)

_ValidationScriptsPartial.cshtml

 @section Scripts {
   <script type="text/javascript" src="~/scripts/js1.js"></script>
   <script type="text/javascript" src="~/scripts/js2.js"></script>
   <script type="text/javascript" src="~/scripts/js3.js"></script>
 }