Asp .NET Core 2.2 Razor页面布局和局部

时间:2019-05-03 11:04:47

标签: html razor-pages asp.net-core-2.2

我正在使用Razor Pages在Asp .NET Core 2.2中构建应用程序。设置了我的登录页面,我想重定向到主页。

   public IActionResult OnGet()
    {
        try
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToPage("/Account/Index");
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }

        return Page();
    }



    public async Task<IActionResult> OnPostAsync()
    {
        try
        {
            // Verification.  
            if (ModelState.IsValid)
            {
                // Dominio LDAP
                string domain = "sd6.glb.corp.local";

                // Verificação de autenticação na AD. 
                var isValid = LdapAuthenticate(domain, login.Username, login.Password);

                if (isValid)
                {
                    var user = GetUser();

                    // Login.  
                    await SignInUser(user, login.RememberMe);

                    // Info.  
                    return RedirectToPage("/Account/Index");
                }
                else
                {
                    // Setting.  
                    ModelState.AddModelError(string.Empty, "Username / Password Incorrecto(s).");
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }

        return Page();
    }

问题是当我重定向到“帐户/索引”页面

我得到这个例外

  

InvalidOperationException:模型项传递到   ViewDataDictionary是类型   'Abastecimento.Pages.Account.IndexModel',但此ViewDataDictionary   实例需要类型的模型项   “ Abastecimento.Pages.Shared.Pages_Shared__NavbarPartial”。

这是我的布局

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Abastecimento</title>

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/lib/adminlte/dist/css/adminlte.css" />        
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute"
              crossorigin="anonymous"
              integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"/>
    </environment>
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body class="sidebar-mini">

    <div class="wrapper">
        <!-- Navbar -->
        <partial name="_NavbarPartial" />
        <!-- /.navbar -->
        <partial name="_SidebarPartial" />
        <!-- Main Sidebar Container -->
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <div class="content-header">
                <div class="container-fluid">
                    @*<div class="row mb-2">
                        <div class="col-sm-6">
                            <h1 class="m-0 text-dark">Starter Page</h1>
                        </div><!-- /.col -->
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item active">Starter Page</li>
                            </ol>
                        </div><!-- /.col -->
                    </div><!-- /.row -->*@
                </div><!-- /.container-fluid -->
            </div>
            <!-- /.content-header -->
            <!-- Main content -->
            <div class="content fade-in">
                <div class="container-fluid my-5 pb-4">

                    @RenderBody()

                </div><!-- /.container-fluid -->
            </div>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->
        <!-- Main Footer -->
        <partial name="_FooterPartial" />   
    </div>


    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
        <script src="~/lib/adminlte/dist/js/adminlte.js"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
        </script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o">
        </script>
    </environment>

    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>

这是我的导航栏的一部分

@page

<!-- Navbar -->
<nav class="main-header navbar navbar-expand navbar-dark bg-danger border-bottom custom-sidebar fixed-top">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
        <li class="nav-item ">
            <a class="nav-link custom-nav-link custom-navbar-links" data-widget="pushmenu" href="#"><i class="fas fa-bars"></i></a>
        </li>
        <li class="nav-item d-none d-sm-inline-block">

            <a href="/Account/Index" class="nav-link custom-navbar-links">
                <p>
                    <i class="fas fa-home"></i>
                    Início
                </p>
            </a>

        </li>
        <li class="nav-item d-none d-sm-inline-block">

            <a href="/Account/Home/Suporte" class="nav-link custom-navbar-links">
                <p>
                    <i class="fas fa-question"></i>
                    Suporte
                </p>
            </a>

        </li>
    </ul>

    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">

        <li class="nav-item">
            <a class="nav-link custom-navbar-links" href="/Account/Logout">
                <i class="fas fa-sign-out-alt"></i>
                Terminar Sessão
            </a>
        </li>
    </ul>
</nav>
<!-- /.navbar -->

这在使用部分视图的asp net MVC 5上可以正常工作,但我不知道是什么导致了此错误。

1 个答案:

答案 0 :(得分:0)

通常,您应该从登录页面重定向到首页/索引。 如果您发送“索引”页面,我可以告诉您更好的原因。