0

任何人都可以帮助解决这个问题吗,我在发布到网络主机之前已经将网站发布到本地 iis7,现在我的数据库连接或应用程序设置都没有被拾取,因此我得到了

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

我的网站在一个包含 2 个项目的解决方案中 1 个项目是一个包含数据库数据和调用远程站点的类库,第二个项目包含我所有的 ui 数据、控制器助手等。

所以我有 Web.Domain 和 Web.UI。

如果我有如下的基本课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Web.Domain.EAN
 {
public class testFromDomain : ItestFromDomain
    {
   public string testdata()
       {
       return "test from domain";
       }
    }
}

这工作正常,这就是为什么我认为我的配置文件有问题

有谁知道我做错了什么或错过了什么。

我的网站是 MVC4

------------------完全错误------------------

“/”应用程序中的服务器错误。

你调用的对象是空的。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例。] Web.UI.Helpers.Weather.GetWeatherCurrentForcast.data(String id) +1947 System.Threading.Tasks.Task`1.InnerInvoke() +78 System.Threading .Tasks.Task.Execute() +109 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) +144 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) +84 Web.UI.Controllers.d__3.MoveNext( )+462 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+144 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+84 lambda_method(闭包,任务)+57 System.Web.Mvc.Async.TaskAsyncActionDescriptor .EndExecute(IAsyncResult asyncResult) +105 System.Web.Mvc.Async.<>c_DisplayClass3f.b _3e(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.<>c_ DisplayClass39.b _33() +124 System.Web.Mvc.Async.<>c_ DisplayClass4f.b _49() +838059 系统。 Web.Mvc.Async.<>c_ DisplayClass37.b _36(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.<>c_ DisplayClass2a.b _20() +33 System.Web.Mvc.Async.<>c_ DisplayClass25 .b _22(IAsyncResult asyncResult) +838644 System.Web.Mvc.<>c_ DisplayClass1d.b _18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.<>c_ DisplayClass4.b _3(IAsyncResult ar) +15 System .Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65 System.Web.Mvc.Async.<>C_DisplayClass4.b _3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51 System.Web.Mvc.<>c_ DisplayClass8.b _3(IAsyncResult asyncResult) +42 System.Web.Mvc。 Async.<>c_ DisplayClass4.b _3(IAsyncResult ar) +15 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +282


版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.18034

---------------------访问者评论的完整代码------------------

    using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace Web.Domain.VisitorReviews
    {
    public class DisplayVisitorReviews
        {
        public string ForumImageID      { get; private set; }
        public string ForumPostTitle    { get; private set; }
        public string ForumCity         { get; private set; }
        public string ForumRegion       { get; private set; }
        public string ForumCountry      { get; private set; }
        public string ForumPost         { get; private set; }
        public string ForumRatePost     { get; private set; }
        public string ForumMember       { get; private set; }
        public string ForumImageType    { get; private set; }
        public byte[] ForumImage        { get; private set; }

                  public DisplayVisitorReviews(string ForumImageID,
                                               string ForumPostTitle,
                                               string ForumCity,
                                               string ForumRegion,
                                               string ForumCountry,
                                               string ForumPost,
                                               string ForumRatePost,
                                               string ForumMember,
                                               string ForumImageType,
                                               byte[] ForumImage)
            {
            this.ForumImageID       = ForumImageID;
            this.ForumPostTitle     = ForumPostTitle;
            this.ForumCity          = ForumCity;
            this.ForumRegion        = ForumRegion;
            this.ForumCountry       = ForumCountry;
            this.ForumPost          = ForumPost;
            this.ForumRatePost      = ForumRatePost;
            this.ForumMember        = ForumMember;
            this.ForumImageType     = ForumImageType;
            this.ForumImage         = ForumImage;
            }
        }
    public class GetVisitorReviews : IGetVisitorReviews
        {
        private string dbConn;

        public GetVisitorReviews()
            {
            dbConn = ConfigurationManager.ConnectionStrings["Website"].ConnectionString;
            }
        public IEnumerable<DisplayVisitorReviews> DisplayTopTenVisitorReviews(string id)
            {
            //string cacheTopTenDestinationReports        = "TopTenDestinationReports + "-"+ id";
            //string TopTenDestinationReportsCacheKey     = cacheTopTenDestinationReports;
            //MemoryCache CacheTopTenDestinationReports   = MemoryCache.Default;

            //if (CacheTopTenDestinationReports.Contains(TopTenDestinationReportsCacheKey))
            //    return CacheTopTenDestinationReports[TopTenDestinationReportsCacheKey] as IEnumerable<DisplayVisitorReviews>;

            string spName = "dbo.GetTopTenForumPosts";
            using (SqlConnection cn = new SqlConnection(dbConn))
                {
                using (SqlCommand cmd = new SqlCommand(spName, cn))
                    {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@strLocation", SqlDbType.VarChar, 150));
                    cmd.Parameters["@strLocation"].Value = id;

                    List<DisplayVisitorReviews> lst = new List<DisplayVisitorReviews>();
                    try
                        {
                        cn.Open();
                        using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default))
                            {
                            if (rdr.HasRows)
                                {
                                while (rdr.Read())
                                    {
                                    DisplayVisitorReviews GetData = new DisplayVisitorReviews(
                                                                                   Convert.ToString((Guid)rdr["ForumImageID"]),
                                                                                   (string)rdr["ForumPostTitle"],
                                                                                   (string)rdr["ForumCity"],
                                                                                   (string)rdr["ForumRegion"],
                                                                                   (string)rdr["ForumCountry"],
                                                                                   (string)rdr["ForumPost"],
                                                                                   Convert.ToString((int)rdr["ForumRatePost"]),
                                                                                   (string)rdr["ForumMember"],
                                                                                   (string)rdr["ForumImageType"],
                                                                                   (byte[])rdr["ForumImage"]);
                                    lst.Add(GetData);
                                    }
                                }
                            }

                        //CacheItemPolicy policy = new CacheItemPolicy();
                        //policy.AbsoluteExpiration = DateTime.Now.AddHours(1);
                        //CacheTopTenDestinationReports.Add(TopTenDestinationReportsCacheKey, lst.AsEnumerable(), policy);

                        return lst;

                        }
                    catch (Exception ex)
                        {
                        throw new ApplicationException(ex.InnerException.ToString());
                        }
                    }
                }
            }
        }
    }



using System;
using System.Collections.Generic;
namespace Web.Domain.VisitorReviews
    {
    public interface IGetVisitorReviews
        {
        IEnumerable<DisplayVisitorReviews> DisplayTopTenVisitorReviews(string id);
        }
    }


kernel.Bind<IGetVisitorReviews>().To<GetVisitorReviews>().InSingletonScope();

 private readonly IGetVisitorReviews _IGVR;

    public MembersController(IGetVisitorReviews IGVR)
        {
        _IGVR = IGVR;
        }

public PartialViewResult pvVisitorReviews()
            {

                var destinations = _IGVR.DisplayTopTenVisitorReviews("NA").Take(5);
                return PartialView("pvHomePageVisitorReviews",destinations);
            }

 <add name="Website" connectionString="Data Source=Windows7\SQL2012;Initial Catalog=WebsiteDB;Persist Security Info=true; Integrated Security=SSPI" providerName="System.Data.SqlClient" />

我已经为评论添加了完整的代码,原因是代码量最短。此外,如果我注释掉天气部分视图,则相同的错误会出现在评论中,如果我注释掉评论或如果从 web.config 引用站点加载的任何其他内容。

------------------------在类中环绕连接设置后出现完全错误------------

Server Error in '/' Application.
--------------------------------------------------------------------------------


 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.

Source Error: 





Line 87:                 <div class="dvAsyncDestination">
Line 88:                    
Line 89:                   @{Html.RenderAction("pvVisitorReviews", "Members", true);}
Line 90:                    
Line 91:                 </div> 

Source File: f:\LocalTestSite\Views\Home\Index.cshtml    Line: 89 

Stack Trace: 





[NullReferenceException: Object reference not set to an instance of an object.]
   Web.UI.Controllers.MembersController.pvVisitorReviews() +184
   lambda_method(Closure , ControllerBase , Object[] ) +78
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +260
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +38
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +33
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +123
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +838059
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +838644
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +15
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +41
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1783

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2819
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +275
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +94
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +700
   DevTrends.MvcDonutCaching.HtmlHelperExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, Boolean excludeFromParentCache) +233
   DevTrends.MvcDonutCaching.HtmlHelperExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName, Boolean excludeFromParentCache) +17
   ASP._Page_Views_Home_Index_cshtml.Execute() in f:\LocalTestSite\Views\Home\Index.cshtml:89
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +126
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +181
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +854172
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +838644
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034 
4

2 回答 2

1

我已经解决了这个问题,只用了 1 天 :-( 对于遇到此问题的任何其他人,请查看此链接Login failed for user 'IIS APPPOOL\ASP.NET v4.0'

上面发布的链接让我走上了正确的轨道,但微软建议使用 ApplicationPoolIdentity 所以这就是我为解决问题所做的。

  1. 打开 SQL,然后打开 Security
  2. 单击登录名并右键单击并选择新登录名
  3. 给它起一个名字 IIS APPPOOL\ASP.NET v4.0
  4. 然后默认数据库选择你想要的数据库
  5. 单击服务器角色并选择公共
  6. 单击用户映射并选择所有者和公共,(我必须选择两者才能使其工作)
  7. 单击确定然后退出安全性
  8. 现在单击您的数据库并选择安全性
  9. 点击用户并选择 IIS APPPOOL\ASP.NET v4.0
  10. 右键单击选择属性
  11. 单击拥有的架构,然后选择数据读取器和数据写入器
  12. 单击成员资格并选择所有者

单击确定,现在返回 IIS 管理器并单击浏览网站,您的站点现在应该在 IIS 中运行。

3分我不满意。

  1. 在用户映射中选择所有者,这真的需要选择吗,如果不是,我怎样才能更好地保护连接
  2. 再次在会员中选择所有者,为什么我需要选择它才能使其正常工作。
  3. 最后,我读过的所有书籍都没有提到必须将 IIS APPPOOL\ASP.NET v4.0 添加到数据库安全性中。如果这是让它发挥作用的唯一方法,请来吧作者,请将此信息添加到您的书籍中,它会为我节省一天的工作时间。

我不是专业的程序员,我只是通过试错和阅读书籍来学习。因此,如果任何其他人可以添加到上述内容,请这样做。

谢谢

乔治

于 2013-02-22T09:10:27.560 回答
0

当您发布您的网站时,它只发布您的网站项目的 Web.config,而不是您的类库项目之一。您必须手动将连接字符串添加到您网站的 Web.config。

于 2013-02-20T19:24:23.037 回答