我有一个带有Oracle后端的相当简单的MVC5应用程序,该程序在Localhost上运行良好,但是发布到内部服务器时有一些奇怪的行为。最初,我得到了一个直接的500-内部服务器错误,我通过尝试直接从服务器内部访问站点来诊断出该错误。出于某种原因,发布时我的Web.Config将添加第二个容器。
在发布前呈现:
<membership>
<providers>
<remove name="OracleMemberShipProvider"/>
</providers>
</membership>
....
<customErrors mode="Off"/>
...
发布后:
<membership>
<providers>
<remove name="OracleMemberShipProvider"/>
</providers>
</membership>
//....
<customErrors mode="Off"/>
<membership>
<providers>
<clear />
</providers>
</membership>
我不确定为什么现在会烦人(不是以前),但是我可以通过在发布完成后简单地登录服务器并从Web.Config中删除重复项来解决此问题。
通过上述更正,我可以成功加载应用程序的主视图。但是,如果单击按钮导航到任何简单的控制器操作(“编辑/创建/删除”),则会收到:
Server Error in '/' Applicaiton. Object reference not set to an instance of an object.
示例包括:// projectRedirect / MY_CONTROLLER / Edit / 78
// projectRedirect / MY_CONTROLLER /创建
// projectRedirect / MY_CONTROLLER / Delete
由于删除是最简单的方法,因此我将在下面详细说明:
// GET: MY_Controller/Delete/5
public async Task<ActionResult> Delete(int? id)
{
// Save the current URL (with any Filter Conditions) to return user after operation completes.
Session["returnURL"] = Request.UrlReferrer.AbsoluteUri;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MY_MODEL my_Model = await db.MY_MODEL.FindAsync(id);
if (my_Model == null)
{
return HttpNotFound();
}
// Set property [OWNER] (ex.) 4 as [OWNER_NAME] (ex.) "SMITH, BOB" - cannot specify on the Grid Component, have to do in Controller.
my_Model.OWNER = my_Model.Owner_Name;
return View(my_Model);
}
// POST: MY_Controller/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Delete(int id)
{
MY_MODEL my_Model = await db.MY_MODEL.FindAsync(id);
db.MY_MODEL.Remove(my_Model);
await db.SaveChangesAsync();
// return RedirectToAction("Index", "Home");
var returnURL = (Session["returnURL"] != null) ? Session["returnURL"].ToString() : Url.Action("Index", "Home");
return Redirect(returnURL);
}
我以为下面的代码行可能是罪魁祸首(尽管以前没有问题)。它仅存储用户当前拥有的URL(可以为指定的数据网格具有过滤条件):
// Save the current URL (with any Filter Conditions) to return user after operation is completed.
Session["returnURL"] = Request.UrlReferrer.AbsoluteUri;
在这种情况下,我所有相关的操作中都包含此行。我尝试将其删除,但是再次进行Publish / Web.Config修改后,尝试导航到任何操作方法时仍会收到错误消息,尽管其中包含有关源错误的更多信息:
删除-在我的
@Html.AntiForgeryToken()
中找到标志@using (Html.BeginForm()) { .... }
创建-标志
@Html.ValidationMessageFor(Model => model.FIELD8, "", new { @class = "text-danger" })
编辑-标志
@htmal.TextBoxFor(model => model.FIELD16, new { @class = "form-control", @readonly = "readonly" })
以下是我提供的堆栈跟踪,尝试移至我的DELETE视图:
[NullReferenceException: Object reference not set to an instance of an object.]
Project.Controllers.<Delete>d__29.MoveNext() +163
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +143
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +23
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +112
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +32
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +231
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
有人知道怎么修这个东西吗?我已经绞尽脑汁,但是对于为什么突然发生这种情况或如何解决它一无所知。
编辑:
因此,经过大量测试,我现在有了一些新信息(但没有真正的答案)。我仍然不知道的事情:
为什么在发布时将多余的> membership
关于新信息,我已经确认所有其他发布的问题似乎都与我对会话变量的使用有关。
当我在“编辑/删除/创建控制器动作”中未注释以下代码行时-
Session["returnURL"] = Request.UrlReferrer.AbsoluteUri;
-如前所述,当尝试加载任何“编辑/删除/创建视图”时,会得到一个平坦的Server Error in '/' Application. Object reference not set to an instance of an object
。在调查我的视图时,我发现如果我还从下面的代码段中注释掉每个视图的以下内容,则将加载所有视图:
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@*@Html.ActionLink("Back to List", "Index", new { controller = "Home" }, new { @class = "btn btn-default" })*@
@*<a href="@Session["returnURL"].ToString()"><span class="btn btn-default">Back to List</span></a>*@ |
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
是否有人想到我的发布服务器不希望我使用此Session变量(但localhost处理得很好)还是可以尝试的替代方法?此特定值对于确保操作完成后确保我的用户返回到主视图的URL(具有在数据网格的URL中指定的任何/所有指定过滤条件)非常重要。
最佳答案
我终于找到了答案。首先,我发现在应用程序部署后,由于在Web.config
中指定了一些Insert
代码,因此我在Web.Release.Config
中得到了额外的代码:
<!--<membership xdt:Transform="Insert">
<providers>
<clear/>
</providers>
</membership>
<roleManager xdt:Transform="Insert">
<providers>
<clear/>
</providers>
</roleManager>-->
已注释掉插入物,现在发布后无需手动修改
Web.config
。接下来,在尝试访问我的
NullReferenceException
时,我在此帖子(The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2))中找到了接收到的Session["variables"]
的答案;我只是修改了我的<modules>
部分以对以下指定值进行add/remove
: <configuration>
...
<system.webServer>
...
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
...
</modules>
</system.webServer>
</configuration>
我仍然不能完全确定为什么这是答案,但是在这种情况下,它可以解决我的问题。