问题:“调用目标已抛出异常”! [英] Problem: "Exception has been thrown by the target of an invocation"!?

查看:73
本文介绍了问题:“调用目标已抛出异常”!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了自己的注册表并注销了sistem。但是当我运行我的MVC应用程序并选择Logout选项时,Visual Studio会抛出异常:调用目标抛出了异常;在这行代码中:



 LazyInitializer.EnsureInitialized( ref  _initializer , ref  _isInitialized, ref  _initializerLock); 




$ z $ b在InitializeSimpleMembershipAttribute.cs中,Visual Studio自动创建。



LogOff控制器:

  public  ActionResult LogOff()
{
WebSecurity.Logout();
return RedirectToAction( Index Home);
}





部分档案:

 <%@     Page    语言  =  C#   继承  =   System.Web.Mvc.ViewPage    %>  
<% if (Request.IsAuthenticated){< span class =code-pagedirective>%>
Hello,<%:User.Identity.Name %> <%:Html.ActionLink( Logout LogOff 帐户%>
< ul < span class =code-attribute> id
= menu >
< li > <%:Html.ActionLink( Home 索引,< span class =code-string> Home%> < / li >
< li > <%:Html.ActionLink( 添加发票 CreateInvoice Bank%> < / li >
< / ul >
<%} 其他 {%>
< < /跨度> <温泉n class =code-leadattribute> ul id = 菜单 >
< li > <%:Html.ActionLink( Home 索引 Home%> < / li >
< / ul >
<% } %>





寄存器控制器:

 [HttpPost] 
public ActionResult RegisterUser(Models.Register用户)
{
if (ModelState.IsValid)
{
注册r = new 注册();
#region RegistrationData
r.Address = user.AddressField;
r.City = user.CityField;
r.Country = user.CountryField;
r.Email = user.EmailField;
r.Name = user.NameField;
r.PersonalIdentificationNumber = user.IDField;
r.Post = user.PostField;
r.TaxNumber = user.TaxNumberField;
r.Type = user.AccountTypeField;
#endregion

BankAccount ba = BaService.createBankAccount(r);
FormsAuthentication.RedirectFromLoginPage(ba.Email, false );

return RedirectToAction( 索引 Home);
}

else
{
ModelState.AddModelError( fdsafdsa< /跨度>);
}

return 查看(用户);
}





如果有人知道如何解决这个或我如何实现这个注销系统它会工作..请帮助我。谢谢!

解决方案

我自己的解决方案是:



- 部分文件

<![CDATA [<%@       语言  =  C#   继承  =  System.Web.Mvc.ViewPage   %> ]]> 
<![CDATA [ <%if(请求.IsAuthenticated){%> ]]>
您好,< ;%:User.Identity.Name %> <%:Html.ActionLink ( Logout Logout 用户%>


< ul id = menu >
< li > <![CDATA [ <%:Html.ActionLink(Domov,Index,Home)%> ]]> < / li >
< li > <![CDATA [ <%:Html.ActionLink(Dodajračun,CreateInvoice,Bank)%> ]]> < / li >
< / ul >
<![CDATA [ < span class =code-summarycomment><%} else {%> ]]>
< ul id = menu >
< li > <![CDATA [ <% :Html.ActionLink(Domov,Index,Home)%> ]]> < / li >
< / ul >
<![CDATA [ <%}%> ]]>





- 控制器

  public  ActionResult Logout()
{
FormsAuthentication.SignOut();

return RedirectToAction( 索引 Home);
}





谢谢! :)


Hi,
i have created my own register and log off sistem. But when i run my MVC application and choose Logout options, Visual Studio throw me an exception: Exception has been thrown by the target of an invocation; in this line of code:

LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);



in InitializeSimpleMembershipAttribute.cs, which Visual Studio automatic created.

LogOff Controller:

public ActionResult LogOff()
{
    WebSecurity.Logout();
    return RedirectToAction("Index", "Home");
}



Partial file:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<% if (Request.IsAuthenticated) { %>
Hello, <%: User.Identity.Name%> <%: Html.ActionLink("Logout", "LogOff", "Account")%>
<ul id="menu">
    <li><%: Html.ActionLink("Home", "Index", "Home") %></li>
    <li><%: Html.ActionLink("Add invoice", "CreateInvoice", "Bank")%></li>
</ul>
<% } else { %>
<ul id="menu">
    <li><%: Html.ActionLink("Home", "Index", "Home") %></li>
</ul>                           
<% } %>



Controller for register:

[HttpPost]
public ActionResult RegisterUser(Models.Register user)
{
    if (ModelState.IsValid)
    {
        Registration r = new Registration();
        #region RegistrationData
        r.Address = user.AddressField;
        r.City = user.CityField;
        r.Country = user.CountryField;
        r.Email = user.EmailField;
        r.Name = user.NameField;
        r.PersonalIdentificationNumber = user.IDField;
        r.Post = user.PostField;
        r.TaxNumber = user.TaxNumberField;
        r.Type = user.AccountTypeField;
        #endregion

        BankAccount ba = BaService.createBankAccount(r);
        FormsAuthentication.RedirectFromLoginPage(ba.Email, false);

        return RedirectToAction("Index","Home");
    }

    else
    {
        ModelState.AddModelError("","fdsafdsa");
    }

    return View(user);
}



If anyone has any idea how i can fix this or how i can implements this logout sistem it will work.. please help me. Thanks!

解决方案

My own solution is:

- partial file

<![CDATA[<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>]]>
<![CDATA[<% if (Request.IsAuthenticated) { %>]]>
Hello, <%: User.Identity.Name%> <%: Html.ActionLink("Logout", "Logout", "User")%>


<ul id="menu">
    <li><![CDATA[<%: Html.ActionLink("Domov", "Index", "Home") %>]]></li>
    <li><![CDATA[<%: Html.ActionLink("Dodaj račun", "CreateInvoice", "Bank")%>]]></li>
</ul>
<![CDATA[<% } else { %>]]>
<ul id="menu">
    <li><![CDATA[<%: Html.ActionLink("Domov", "Index", "Home") %>]]></li>
</ul>                           
<![CDATA[<% } %>]]>



- controller

public ActionResult Logout()
{
    FormsAuthentication.SignOut();

    return RedirectToAction("Index", "Home");
}



Thanks! :)


这篇关于问题:“调用目标已抛出异常”!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆