【问题标题】:MVC C# Application error to show messagebox.show [closed]MVC C#应用程序错误显示messagebox.show [关闭]
【发布时间】:2015-08-26 14:14:20
【问题描述】:

在 Visual Studio 2015 中运行我的 MVC4 应用程序时,她在浏览器中正常运行并运行所有内容,ultilizo MessageBox.Show 在代码中间,在某些地方使用相同的,通常没有问题。

我在开发 Framework 4.6 时使用了它,我将它安装在我的机器上,这将是服务器 Enforcement,我的 IIS 7,它是我的 UserAppPool 只有框架 v4.0.30319。

我不需要使用 messageBox,但这是我找到的唯一解决方案,还有其他选项可以为用户显示消息吗?

当您在浏览器中打开应用程序时,会正常打开单击按钮以执行操作并生成消息框,它会生成以下错误:

Erro de Servidor no Aplicativo '/'.

Mostrar um formulário ou uma caixa de diálogo restrita quando o aplicativo não está no modo UserInteractive não é uma operação válida. Especifique o estilo ServiceNotification ou DefaultDesktopOnly para exibir uma notificação de um aplicativo de serviços.

Descrição: Ocorreu uma exceção sem tratamento durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código. 

Detalhes da Exceção: System.InvalidOperationException: Mostrar um formulário ou uma caixa de diálogo restrita quando o aplicativo não está no modo UserInteractive não é uma operação válida. Especifique o estilo ServiceNotification ou DefaultDesktopOnly para exibir uma notificação de um aplicativo de serviços.

Erro de Origem: 


Linha 1:  @{
Linha 2:      ViewBag.Title = "Home Page";
Linha 3:  }
Linha 4:  <br />

Arquivo de Origem: C:\inetpub\wwwroot\mm_solution\Views\Home\Index.cshtml    Linha: 2 

Rastreamento de Pilha: 


[InvalidOperationException: Mostrar um formulário ou uma caixa de diálogo restrita quando o aplicativo não está no modo UserInteractive não é uma operação válida. Especifique o estilo ServiceNotification ou DefaultDesktopOnly para exibir uma notificação de um aplicativo de serviços.]
   System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp) +2994248
   MM_Solution_2.Controllers.HomeController.SendMessage(String test) +1609
   lambda_method(Closure , ControllerBase , Object[] ) +104
   System.Web.Mvc.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +22
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +10
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +27
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +64
   System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +71
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1556

[HttpException (0x80004005): Erro ao executar a solicitação filho do manipulador '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) +3424518
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +77
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +29
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +24
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +463
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +83
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName) +9
   ASP._Page_Views_Home_Index_cshtml.Execute() in C:\inetpub\wwwroot\mm_solution\Views\Home\Index.cshtml:2
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +256
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9711525
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

以下是浏览器中的信息:

Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.6.81.0

【问题讨论】:

  • asp.net 应用程序中不能有 MessageBox
  • 不理解问题,无法阅读错误,并且您正在尝试在 Web 应用程序中使用 MessageBox。抱歉,这里没有任何意义。
  • @stuartd 不,我不需要使用 messageBox,但这是我找到的唯一解决方案,您还有另一个选项可以为用户显示消息?
  • @David 不,我不需要使用 messageBox,但这是我找到的唯一解决方案,您还有另一个选项可以为用户显示消息?
  • @JhonnyPamponet:你将不得不展示你尝试过的东西并解释它是如何没有按预期工作的。目前我给你的唯一建议是从一些关于 Web 开发和 ASP.NET MVC 的介绍性教程开始。

标签: c# asp.net visual-studio asp.net-mvc-4 iis


【解决方案1】:

基于到目前为止的评论线程......

如果您只想从控制器向视图发送“消息”,您可以将其设置为模型的属性:

someModel.SomeProperty = "Some Message";

或者,在最坏的情况下,作为ViewData 之类的一部分:

ViewData["SomeProperty"] = "Some Message";

然后,在视图中,您将显示它。理想情况下,这将比alert() 更优雅。您可以将其包含在标记中:

@if(!string.IsNullOrEmpty(Model.SomeProperty)) {
    <div id="message">@Model.SomeProperty</div>
}

然后您可以将任何您想要的样式或效果应用到该div。但是,如果您真的想使用alert(),那也是一样的:

@if(!string.IsNullOrEmpty(Model.SomeProperty)) {
    <script type="text/javascript">
        alert('@Model.SomeProperty');
    </script>
}

(在将 JavaScript 代码放入 Razor 块时,您可能需要明智地使用 &lt;text&gt;&lt;/text&gt; 标签。有时语法会混淆渲染引擎。)

【讨论】:




  • @if (!string.IsNullOrEmpty(Model.SomeProperty)) { }
  • 但显示错误消息System.Core.dll 中发生“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理其他信息:Não é possível fazer associação em tempo de execução em uma referência nula
  • @JhonnyPamponet:那么,您的模型是否有一个名为SomeProperty 的属性? (或者你决定怎么称呼它。)另外,我看不懂那个错误信息,所以我只能猜测它说的是什么。您似乎也错误地使用了&lt;text&gt; 标签,尽管我不能仅通过查看它来确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多