1. <tfoot id='OWZly'></tfoot>
  2. <small id='OWZly'></small><noframes id='OWZly'>

  3. <i id='OWZly'><tr id='OWZly'><dt id='OWZly'><q id='OWZly'><span id='OWZly'><b id='OWZly'><form id='OWZly'><ins id='OWZly'></ins><ul id='OWZly'></ul><sub id='OWZly'></sub></form><legend id='OWZly'></legend><bdo id='OWZly'><pre id='OWZly'><center id='OWZly'></center></pre></bdo></b><th id='OWZly'></th></span></q></dt></tr></i><div id='OWZly'><tfoot id='OWZly'></tfoot><dl id='OWZly'><fieldset id='OWZly'></fieldset></dl></div>

  4. <legend id='OWZly'><style id='OWZly'><dir id='OWZly'><q id='OWZly'></q></dir></style></legend>
      <bdo id='OWZly'></bdo><ul id='OWZly'></ul>

      仅访问模型时的 ASP.NET Core NullReferenceException

      时间:2023-07-10
        <i id='8nF4S'><tr id='8nF4S'><dt id='8nF4S'><q id='8nF4S'><span id='8nF4S'><b id='8nF4S'><form id='8nF4S'><ins id='8nF4S'></ins><ul id='8nF4S'></ul><sub id='8nF4S'></sub></form><legend id='8nF4S'></legend><bdo id='8nF4S'><pre id='8nF4S'><center id='8nF4S'></center></pre></bdo></b><th id='8nF4S'></th></span></q></dt></tr></i><div id='8nF4S'><tfoot id='8nF4S'></tfoot><dl id='8nF4S'><fieldset id='8nF4S'></fieldset></dl></div>

          <bdo id='8nF4S'></bdo><ul id='8nF4S'></ul>

              <tbody id='8nF4S'></tbody>
          1. <tfoot id='8nF4S'></tfoot>

                <small id='8nF4S'></small><noframes id='8nF4S'>

                <legend id='8nF4S'><style id='8nF4S'><dir id='8nF4S'><q id='8nF4S'></q></dir></style></legend>
                本文介绍了仅访问模型时的 ASP.NET Core NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在尝试使用强类型模型创建视图时遇到问题.无论我将什么作为模型传递给 View(),即使只是访问 Model,我总是会收到 NullReferenceException.p>

                在执行剃刀页面的其余部分之前,我什至无法检查模型是否为空;简单地执行 if (Model != null) 也会抛出相同的 NullReferenceException.

                Index.cshtml

                model 编码模型@{布局 = "~/Pages/Shared/_Layout.cshtml";}<h2>编码</h2><div id="进度">@await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())</div>

                EncodeProgress.cshtml

                model FFenc.IEncoderModule@{var模块=模型;//这会抛出 NullReferenceException}

                Startup.cs

                 public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/错误");应用程序.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseCookiePolicy();app.UseMvc();}

                异常堆栈跟踪:

                <块引用>

                NullReferenceException:对象引用未设置为对象的实例.

                AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
                EncodeProgress.cshtml 中的 AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync()
                var 模块 = 模型;

                我做错了什么?我尝试了多种修复和解决方法(使用 ViewComponent 而不是视图),但没有任何效果.

                我发现的一些类似问题并没有解决我的问题:

                ASP.NET Core Model null 错误索引视图

                我已经传递了模型,所以这个答案不会改变我正在做的任何事情.例如,当我尝试使用控制器作为解决方法时,同样的 NullReferenceException 发生在这段代码中:

                 [Route("/encode/progress")]公共 IActionResult GetProgress(){return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());}

                解决方案

                我认为你在混合 Razor 页面 与 ASP.NET MVC.尝试删除 @page 指令,您的模型应该绑定到调用 return View() 时传递的值.

                I am having trouble with attempting to create a view with a strongly typed model. No matter what I pass in as the model to a View(), I always receive a NullReferenceException when even just accessing the Model.

                I can't even check if the model is null before executing the rest of the razor page; simply doing a if (Model != null) also throws the same NullReferenceException.

                Index.cshtml

                @page
                @model EncodeModel
                @{
                    Layout = "~/Pages/Shared/_Layout.cshtml";
                }
                
                <h2>Encode</h2>
                
                <div id="progress">
                    @await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())
                </div>
                

                EncodeProgress.cshtml

                @page
                @model FFenc.IEncoderModule
                
                @{
                    var module = Model; //this throws the NullReferenceException
                }
                

                Startup.cs

                    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
                    {
                        if (env.IsDevelopment())
                        {
                            app.UseDeveloperExceptionPage();
                        }
                        else
                        {
                            app.UseExceptionHandler("/Error");
                            app.UseHsts();
                        }
                
                        app.UseHttpsRedirection();
                        app.UseStaticFiles();
                        app.UseCookiePolicy();
                
                        app.UseMvc();
                    }
                

                Exception stack trace:

                NullReferenceException: Object reference not set to an instance of an object.

                AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
                AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync() in EncodeProgress.cshtml
                var module = Model;

                What am I doing wrong? I have attempted multiple fixes and workarounds (using a ViewComponent instead of a view) but nothing works.

                Some similar questions that I have found that have not solved my problem:

                ASP.NET Core Model null error in the Index view

                I'm already passing in the model so this answer doesn't change anything about what I'm doing. For example, when I was trying to use a controller as a workaround, the same NullReferenceException happened with this code:

                    [Route("/encode/progress")]
                    public IActionResult GetProgress()
                    {
                        return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());
                    }
                

                解决方案

                I think you're mixing Razor Pages with ASP.NET MVC. Try removing the @page directive and your model should be bound to the value passed when you call return View().

                这篇关于仅访问模型时的 ASP.NET Core NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:自定义身份验证类型的登录页面设计:个人用户帐户 ASP.NET core 2.1、MVC、c# 下一篇:如何在 .NET Core 中使用 PROVIDER 读取连接字符串?

                相关文章

                <small id='Gxikt'></small><noframes id='Gxikt'>

                    <bdo id='Gxikt'></bdo><ul id='Gxikt'></ul>
                  <legend id='Gxikt'><style id='Gxikt'><dir id='Gxikt'><q id='Gxikt'></q></dir></style></legend>
                    <tfoot id='Gxikt'></tfoot>
                    <i id='Gxikt'><tr id='Gxikt'><dt id='Gxikt'><q id='Gxikt'><span id='Gxikt'><b id='Gxikt'><form id='Gxikt'><ins id='Gxikt'></ins><ul id='Gxikt'></ul><sub id='Gxikt'></sub></form><legend id='Gxikt'></legend><bdo id='Gxikt'><pre id='Gxikt'><center id='Gxikt'></center></pre></bdo></b><th id='Gxikt'></th></span></q></dt></tr></i><div id='Gxikt'><tfoot id='Gxikt'></tfoot><dl id='Gxikt'><fieldset id='Gxikt'></fieldset></dl></div>