I am experiencing very strange behavior. I am creating a simple User creation module. I have created a very simple models,
public class CreateUser
{
[Required]
public string Email { get; set; }
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
When I access the view, the following exception message is displayed,
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
and sometimes the same page gives this exception,
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
And sometime just this error,
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
I have isolated the problem but I am unable to understand what is problem with my view. Below is my View,
@model Practice_Project.Models.Accounts.CreateUser
@{
ViewBag.Title = "Register";
}
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "validator-form form-horizontal", role = "form" }))
{
<div class="form-group">
<label for="Email" class="col-sm-3">Email</label>
<div class="col-md-9">
<input type="text" class="form-control inputmask" id="Email" name="Email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="username" class="col-sm-3">User Name</label>
<div class="col-md-9">
<input type="text" class="form-control inputmask" id="Username" name="Username" placeholder="User Name">
</div>
</div>
<div class="form-group">
<label for="Password" class="col-sm-3">Password</label>
<div class="col-md-9">
<input type="password" class="form-control" id="Password" name="Password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="ConfirmPassword" class="col-sm-3">Confirm Password</label>
<div class="col-md-9">
<input type="password" class="form-control" id="ConfirmPassword" name="ConfirmPassword" placeholder="Re-Type your Password">
</div>
</div>
<div class="form-group">
<div class="col-md-7">
<button type="submit" class="btn btn-success" value="Validate">Sign In</button>
<span class="pull-right">
<small>Not Register ?</small>
<span>
<a href="@Html.Action("Register", "Account")" class="btn btn-info">Signup Now</a>
</span>
</span>
</div>
</div>
}
When I remove this last piece of code from the form then the exception does not occur,
<div class="form-group">
<div class="col-md-7">
<button type="submit" class="btn btn-success" value="Validate">Register</button>
<span class="pull-right">
<small>Already Registered?</small>
<span>
<a href="@Html.Action("Login", "Account")" class="btn btn-info">SignIn Now</a>
</span>
</span>
</div>
</div>
I don't see anything wrong with this piece of code which is when removed the error is gone. In troubleshooting options it is mentioned that it might be due to Infinite loop but how?
Edit: The controller is very simple and has just two methods for now as I am adding as I move forward. The following is the code for controller,
[Authorize]
public class AccountController : Controller
{
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
}
Note I am trying to generate a link on the Register page that navigates to the Login page.
Login()method in theAccountController? - in which case you do have an infinite loopLogin(), the the view engine would start parsing the view, hit the@Html.Action("Login", "Account")line, call theLogin()again which would start parsing the view again, hit the@Html.Action("Login", "Account")line, call theLogin()again which would start parsing the view again and so on and so on until you run out of memory.