本文介绍了在ASP.NET Core MVC中设置新的登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to set a new landing page for my application. For now it is landing on the standard Index.cshtml inside of the Home folder inside of the Views folder. I want my new landing page to be from this directory:

视图/欢迎/Index.cshtml

错误我正在说:
InvalidOperationException: / Views / Welcome / Index.cshtml中的页面尚未调用RenderBody。忽略调用IgnoreBody();

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

I have made the following changes so far in my startup.cs file:

        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

我的天堂

谢谢!

推荐答案

如果要返回其他视图,您可以尝试在首页/索引中重定向到其他视图。

If you want to return different view in a simply way, you could try redirecting to other view in Home/Index.

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


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

As for the layout, _ViewStart.cshtml(/Pages/Shared) are run before every full view (not layouts, and not partial views).

_ViewStart.cshtml

_ViewStart.cshtml

@{
    Layout = "_Layout";
}


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

In the code below, you define the scripts section and render scripts inside the section.

Index.cshtml

Index.cshtml

 @RenderSection("Scripts", required: true)

_ValidationScriptsPartial.cshtml

_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>
 }

这篇关于在ASP.NET Core MVC中设置新的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:03