Asp.net mvc MVC两个Html.Action在一个视图中
我正在使用MVC 5,最近遇到了一个问题: 我有一个控制器[HomeController]和一个带有2个Partial1和Partial2的视图。 我尝试使用Html.Action命令呈现这两个部分(因为我希望调用相应的操作) 所有操作都在HomeController中 该代码在本地主机和开发环境(Visual Studio 2015)上运行良好。 当我在Azure上发布项目时,在尝试执行视图时出现以下错误:Asp.net mvc MVC两个Html.Action在一个视图中,asp.net-mvc,asp.net-mvc-4,azure,Asp.net Mvc,Asp.net Mvc 4,Azure,我正在使用MVC 5,最近遇到了一个问题: 我有一个控制器[HomeController]和一个带有2个Partial1和Partial2的视图。 我尝试使用Html.Action命令呈现这两个部分(因为我希望调用相应的操作) 所有操作都在HomeController中 该代码在本地主机和开发环境(Visual Studio 2015)上运行良好。 当我在Azure上发布项目时,在尝试执行视图时出现以下错误: A single instance of controller 'HomeContro
A single instance of controller 'HomeController' cannot be used to handle
multiple requests. If a custom controller factory is in use, make sure that
it creates a new instance of the controller for each request.
我尝试在“部分”操作上使用[ChildActionOnly]并返回PartialViewResult,但没有成功
我知道我可以使用RenderPartial,但是,因为我想进行数据库调用来初始化部分,所以我需要使用Html.Action
对这个问题有什么想法吗
代码
看法
我最好的猜测是,您正在使用IoC(控制反转)模式,并且确实使用IoC容器(Castle Windsor、Autofac等)实例化了控制器。这是真的吗?如果是,请确保控制器未注册为单例,或者其生存期不是“每个web请求”。每次需要新控制器时,都应该对其进行实例化,即其寿命应该是短暂的(用温莎城堡的术语来说)。经过长时间的研究和多次测试后,答案总是这么简单: 清理->重建(本地) 发布->选中“在目标位置删除其他文件”(以清除azure上的项目应用程序)
非常感谢你的回答和努力 发布您的代码-
HomeController
,以及所有视图。您是否使用DI(依赖注入/控制反转)容器-例如Castle Windsor?我添加了一些代码和更多信息。这个错误是来自WebAPI项目还是MVC项目?您提到正在使用StructureMap—这是用于MVC和WebAPI,还是仅用于WebAPI?如果是这种情况,最好查看注册。StructureMap仅由WebAPI项目使用。错误来自MVC,而不是WebAPI。而且只有当它发布在Azure上,而不是在Dev Env上时。
public ActionResult ParentView()
{
return View();
}
[ChildActionOnly]
public PartialViewResult _ChildView1()
{
WebApi Client3 = new WebApi();
Guid currentId = Guid.Empty;
if (Session["CurrentId"] != null)
{
currentId = (Guid)Session["CurrentId"];
}
ResponseList retModel = Client3 .GetThingsFromDB(currentId);
return PartialView("~/Views/Home/Folder/_View1.cshtml", retModel);
}
[ChildActionOnly]
public PartialViewResult _ChildView2()
{
WebApi Client3 = new WebApi();
Guid currentId = Guid.Empty;
if (Session["CurrentId"] != null)
{
currentId = (Guid)Session["CurrentId"];
}
ResponseList retModel = Client3.GetThingsFromDB(currentId);
return PartialView("~/Views/Home/Folder/_View2.cshtml", retModel);
}
<div class="seperator">
@{
Html.RenderAction("_ChildView1","Home");
}
</div>
<div class="seperator">
@{
Html.RenderAction("_ChildView2","Home");
}
</div>
using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Facebook;
using Microsoft.Owin.Security.Google;
using Owin.Security.Providers.Instagram;
using Newtonsoft.Json.Linq;
using Owin;
using System;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Helpers;
using System.Configuration;
[assembly: OwinStartup(typeof(Startup))]
namespace WebPortal
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Authorize/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
var fbOptions = new FacebookAuthenticationOptions
{
Provider = new YoohBridgeFacebookAuthenticationProvider(),
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
AppId = ConfigurationManager.AppSettings["facebook.clientid"].ToString(),
AppSecret = ConfigurationManager.AppSettings["facebook.clientsecret"].ToString(),
BackchannelHttpHandler = new FacebookBackChannelHandler(),
UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location"
};
app.UseFacebookAuthentication(fbOptions);
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = ConfigurationManager.AppSettings["google.clientid"].ToString(),
ClientSecret = ConfigurationManager.AppSettings["google.clientsecret"].ToString()
});
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}
}
public class MyFacebookAuthenticationProvider : FacebookAuthenticationProvider
{
public MyFacebookAuthenticationProvider() : base() { }
public override void ApplyRedirect(FacebookApplyRedirectContext context)
{
context.Response.Redirect(context.RedirectUri + "&display=popup");
}
}
public class FacebookBackChannelHandler : HttpClientHandler
{
protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
{
request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
}
return await base.SendAsync(request, cancellationToken);
}
}
}
at System.Web.WebPages.WebPageExecutingBase.NormalizeLayoutPagePath(String layoutPagePath)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)