1

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.

13
  • 1
    Is the method that generated this view the Login() method in the AccountController? - in which case you do have an infinite loop Commented May 21, 2015 at 22:18
  • We're going to need the controller action that renders the view. Commented May 21, 2015 at 22:18
  • Have you put breakpoints in your MVC action methods to see if and how many times it is hitting them? Commented May 21, 2015 at 22:23
  • @Stephen Muecke Kindly see the edit in my question. The method that generated this View is the Register method. I don't understand what do you mean that "In which case you do have an infinite loop". Can you explain a little. Commented May 21, 2015 at 22:34
  • 1
    @AdnanYaseen. If the method that rendered this view was Login(), the the view engine would start parsing the view, hit the @Html.Action("Login", "Account") line, call the Login() again which would start parsing the view again, hit the @Html.Action("Login", "Account") line, call the Login() again which would start parsing the view again and so on and so on until you run out of memory. Commented May 21, 2015 at 23:00

1 Answer 1

3

@Html.Action("Login", "Account") is a method that calls the Login() method of AccountController and renders the partial view it returns. In order to generate a url for a link, you need to use @Url.Action("Login", "Account") although I suggest you use

@Html.ActionLink("SignIn Now", "Login", "Account", null, new { @class = "btn btn-info" })

The reason for the exception (as identified in the comments) was that you had not yet created a view for the Login method (the Login method is trying to return a view which does not exist).

However there are other issues with you code. Your properties are decorated with validation attributes, but because your manually generating the html and omitting the data-val-* attributes, you will not get client side validation, and because you do not have @Html.ValidationSummary() or @Html.ValidationMessageFor() helpers in the form, server side validation errors will not be displayed. Remove the manual html for each property and replace with

<div class="form-group">
    @Html.LablFor(m => m.Email, new { @class = "col-sm-3" })
    <div class="col-md-9">
        @Html.TextBoxFor(m => m.Email, new ( placeholder="Email" })
        @Html.ValidationMessageFor(m => m.Email)
    </div>
<div>

and ditto for the other properties. Also include the relevant files for client side validation (typically by including @Scripts.Render("~/bundles/jquery") and @Scripts.Render("~/bundles/jqueryval") in the view).

Finally, the typical approach is that you decorate methods required authorization with the [Authorize] attribute. If an unauthorized user navigates to the method, they are automatically redirected to the login page which may include a link to the register page, both of which are used to authorize the user. Having a link in the Register page that navigates to the Login page seems unusual (why would a user that is already registered navigate to the register page?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Nicely explained every thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.