未设置为对象实例的对象引用.在MVC布局= null

本文关键字:MVC 布局 null 对象引用 设置 对象 实例 | 更新日期: 2023-09-27 18:03:11

我得到一个奇怪的错误位于

@{
    Layout = null;
}

错误:

对象引用未设置为对象的实例。

描述:在执行过程中发生未处理的异常当前的web请求。请查看堆栈跟踪了解更多信息有关错误及其在代码中的起源的信息。

Exception Details: System。NullReferenceException:对象引用不存在设置为对象的实例。

和堆栈跟踪:

[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

这是这个视图的控制器:

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);
        }
    }
}

这是视图本身:

@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> 

任何帮助都将是感激的,我在这里感到沮丧。谢谢。

未设置为对象实例的对象引用.在MVC布局= null

可能的异常行:

<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,调用任何方法,如Substring()带null会导致异常。