c# - 你调用的对象是空的。在 MVC 布局中 = null

标签 c# asp.net asp.net-mvc exception nullreferenceexception

<分区>

我发现一个奇怪的错误位于

@{
    Layout = null;
}

这是错误:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

和堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]      
ASP._Page_Views_Home_Index_cshtml.Execute() in f:\Web Prog\my work\mcpd\mvc\FilippoPhotography\FP.WebUI\Views\Home\Index.cshtml:4    
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197    
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97     
System.Web.WebPages.StartPage.RunPage() +17    
System.Web.WebPages.StartPage.ExecutePageHierarchy() +62    
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76    
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260    
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115    
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295    
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13    
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23    
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242   
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21    
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177       
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89    
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102    
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57    
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43    
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14    
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47    
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10    
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25    
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47    
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9    
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是我的这个 View 的 Controller :

namespace FP.WebUI.Controllers
{
    public class HomeController : Controller
    {
        private IInfoRepo repo;
        public HomeController(IInfoRepo repoParam)
        {
            repo = repoParam;
        }
        public ViewResult Index()
        {
            Info model = repo.Info.FirstOrDefault();
            return View(model);
        }
    }
}

这是 View 本身:

@model FP.Domain.Entities.Info

@{
    Layout = null;
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>A.Filippo Photography</title>
    <link rel="Stylesheet" type="text/css" href="~/Content/css/whole.css" />
    <link rel="Stylesheet" type="text/css" href="~/Content/css/Homepage.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="~/Content/js/homepage.js" type="text/javascript"></script>
</head>
<body>
    <div id="whole_wrapper">
        <header>
            <div id="follow">
                <a href="http://@Model.Facebook.Substring(Model.Facebook.IndexOf("http://")+1,Model.Facebook.Length)"><img src="~/Content/img/temp.png" alt="facebook"/></a>
                <a href="http://@Model.Twitter.Substring(Model.Twitter.IndexOf("http://")+1,Model.Twitter.Length)"><img src="~/Content/img/temp.png" alt="twitter"/></a>
                <a href="mailto://@Model.Email"><img src="~/Content/img/temp.png" alt="email"/></a>
            </div>
            <div id="iconlabel">
                <a href="~/Views/Home/Index"><img src="~/Content/img/logo.png" alt="logo"/></a>
            </div>
        </header>

        <div id="main_content">
                <div id="slides_wrapper">
                    <a href="gallery.aspx">
                        <div id="gallery_slide">                        
                            <p>Gallery</p>
                        </div>
                    </a>
                    <a href="sessions.aspx">
                        <div id="session_slide">
                            <p>Sessions</p>
                        </div>
                    </a>
                    <a href="offers.aspx">
                        <div id="offers_slide">
                            <p>Offers</p>
                        </div>
                    </a>
                    <a href="about.aspx">
                        <div id="about_slide">
                            <p>About Us</p>
                        </div>
                    </a>
                    <a href="contact.aspx">
                        <div id="contact_slide">
                            <p>Contact Us</p>
                        </div>
                    </a>
                </div>                
            </div>

            <div id="footer">
                <div id="copyright">
                    <div id="rights">
                        <p>All rights reserved.© - Best viewed using the latest version of <a href="http://www.google.com/chrome">Google Chrome</a> or <a href="www.mozilla.org/en-US/firefox/new/">Mozilla Firefox</a>.</p>                    
                    </div>
                    <div id="stamp">                    
                        <p>Crafted by</p>
                        <a href="http://about.me/rafael.adel">Rafael Adel</a>
                    </div>                   
                </div>
            </div>

    </div>   
</body>
</html> 

任何帮助将不胜感激,我在这里感到很沮丧。谢谢。

最佳答案

可能的异常行:

<a href="http://@Model.Facebook.Substring(Model.Facebook.IndexOf("http://")+1,Model.Facebook.Length)"><img src="~/Content/img/temp.png" alt="facebook"/></a>
<a href="http://@Model.Twitter.Substring(Model.Twitter.IndexOf("http://")+1,Model.Twitter.Length)"><img src="~/Content/img/temp.png" alt="twitter"/></a>

可能代码 Info model = repo.Info.FirstOrDefault(); 返回一个默认值,这意味着 null 并且使用 null 调用任何方法,如 Substring() 会导致异常.

关于c# - 你调用的对象是空的。在 MVC 布局中 = null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15312062/

相关文章:

c# - 使用 ZipFile 在 Windows UWP App 中解压 Zip 存档

c# 7.x 使用元组类型的短名称

asp.net - 如何实现 "Don' t 再次显示此消息”

javascript - 响应.写入divIDE

c# - Asp.net WebApi2 默认路由找不到不同的命名 api?

asp.net - asp.NET 中的 TDD 新手,我是否在正确的轨道上编写测试?

c# - JWT header 算法 : is "hs256" the same as "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"

c# - 在 IQueryable 查询中减去 DateTimes

c# - SQL 数据库中的 Controller 异步/等待死锁

c# - Entity Framework 更新不工作