2

When I run the application with ISS after clicking on a class. The browser starts up with this url. http://localhost:50282/

When I click on Index in my folder Account In Views and run the application I get this url: http://localhost:50282/Account/Index

Now on both urls I have a register form that links to an action in my AccountController. When I submit the form in the second case I get this url: http://localhost:50282/Account/register and the register method is run in my AccountController Class and works fine.

In the first case I get this url and error:

Url: http://localhost:50282/register
Error: 404
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /register

I want the url to go to the second url http://localhost:50282/Account/register after clicking register no matter where I clicked before running the application.

Submit form view code:

    @{
        ViewBag.Title = "Index";
    }
    <h1>Register Form </h1>

    <form action="register" method="post">
    <label><i class="" aria-hidden="true"></i> Username </label>
    <input type="text" name="Username" placeholder="Enter User Name"     required="" />
    <br>
    <label><i class="" aria-hidden="true"></i> password </label>
    <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />

    <input type="submit" value="Register">
</form>
4
  • 1
    Can you show us the cshtml for the register link that isn't working? Commented Nov 28, 2018 at 15:18
  • There is no cshtml for the register link. The register method in the account controller returns a different view Commented Nov 28, 2018 at 15:20
  • I just need to see the submit button rendering mate for the form that's not working Commented Nov 28, 2018 at 15:24
  • added to the question Commented Nov 28, 2018 at 15:28

1 Answer 1

3

You need to specify where your form is being submitted to. So change this...

<form action="register" method="post"> 
    <label>
    <i class="" aria-hidden="true"></i> Username </label> 
    <input type="text" name="Username" placeholder="Enter User Name" required="" /> 
    <br> 
    <label>
    <i class="" aria-hidden="true"></i> password </label> 
    <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> <input type="submit" value="Register"> 
</form>

To a html helper for your form...

@using(Html.BeginForm("Register", "Account"))
{

 <label><i class="" aria-hidden="true"></i> Username </label> 
 <input type="text" name="Username" placeholder="Enter User Name" required="" /> 
 <br> 
 <label><i class="" aria-hidden="true"></i> password </label> 
 <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> 
 <input type="submit" value="Register"> 
 }

Also you need to add antiforgery token for security (@html.Antiforgerytoken()). And decorate Register action with [ValidateAntiforgeryToken] attribute.

See this

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

Comments

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.