10

Edited the question as I've found out that the issue isn't inside the razor, but instead in the route

I have a very simple login form, but somehow, when the user presses Login the page goes tot Error404 and it simply does not hit the controller breakpoints for some reason.

   @using (Html.BeginRouteForm("MyCustomRoute", new { controller = "login", action = "verify", FormMethod.Post }))
                 {
                <fieldset class="clearfix">
                    <p><span style="float:none;color:black; font-size:20pt;"></span></p>
                    <p><span style="float:none;color:black; font-size:20pt;"></span></p>
                    <p><span class="fa fa-user"></span>@Html.TextBoxFor(m => m.UserName, new { @class = "form-control", placeholder = "Username", onkeydown = "convertTabtoEnter(this, event)", autofocus = "" })</p> <!-- JS because of IE support; better: placeholder="Username" -->
                    <p>
                        <span class="fa fa-lock"></span>@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password", onkeyup = "convertTabtoEnter()" })
                    </p> <!-- JS because of IE support; better: placeholder="Password" -->

                    <div>
                        <span style="width:48%; text-align:left;  display: inline-block;">
                            <a class="small-text" href="#">
                                @*Forgot
                    password?*@
                            </a>
                        </span>
                        <span style="width:50%; text-align:right;  display: inline-block;"><input type="submit" value="Sign In"></span>
                    </div>
                </fieldset>
                <div class="clearfix"></div>
                }    

And Inside my login controller I have a simple ActionResult named Verify with the 2 params.

    [RoutePrefix("Login")]

public class LoginController : Controller
{
    // GET: Login
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [Route("Verify")] //Matches GET login/verify

    public ActionResult Verify(string username, string password)
    {...}

What exactly am I doing wrong here? It's not like this is rocket science.

Edit2: I've noticed that whenever I change the RouteConfig.cs back to default it works correctly. So, the problem isn't inside the form tags but within the routings. So I've been trying to add a custom route in order to get this working using this sample: Using Html.BeginForm() with custom routes

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "TrailersOverview",
                url: "{TrailersOverview}/{action}/{vendid}",
                defaults: new { controller = "TrailersOverview", action = "Index", vendId = UrlParameter.Optional }
            );

             routes.MapRoute(
                "MyCustomRoute", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                name: "Default",
                url: "{*anything}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

When I remove the routings and I simply bring everything back to default, the controller does get hit. Unfortunately I really need those routings for the rest of the app :(

4 Answers 4

5

There are several things that seems to be wrong:

  • Your Controller is missing in the route Config.
  • Action and Controller names are in wrong order.

RouteConfig:

A default route looks like that:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Form:

If you want to use Tag Helpers, then you have to change your code from:

<form action="@Url.Action("Login", "Verify")" method="post">

to:

<form asp-controller="Login" asp-action="Verify" method="post">

Have a look here.

4
  • Hi Marc, how do you mean that the controller is missing in the route config? Thanks in advance! May 17, 2019 at 8:03
  • Thanks Marc, but what if I can not use the default routeconfig? Application is using Angular after the login. May 17, 2019 at 8:21
  • You can have different routings, but only you can know what is needed... This problem doesn't belong to asp.net mvc. You should search for routing with angular in this case.
    – Marc
    May 17, 2019 at 8:23
  • How is that? I am trying to do the login before any angular gets hit in the first place. I think I will need to google about custom routes rather than diving into angular. May 17, 2019 at 8:26
1
 @using (Html.BeginForm("Verify", "YourControllerName", FormMethod.Post))
{ your form here // }

please try this if it works !

One more thing add name attribute in your input fields which should be similar to the property name in your model like

@Html.TextBoxFor(m => m.UserName, new { @class = "form-control", id="username", name="USERNAME"})

And this USERNAME should be there in your model like:

Public class yourModel{Public string USERNAME{get;set;}}

And use your Model Object in your Action Method to fetch data.

5
  • Ok,let me check this again @Катерина
    – Ricky
    May 17, 2019 at 7:40
  • Please try changing the url in routes to {controller}/{action}/{id} where it is {anything} @Катерина
    – Ricky
    May 17, 2019 at 7:44
  • Still nothing :( This application has been using Angular. I think that's the reason why they changed the routing to anything. neverless, I changed it as you suggested to try, but didnt work. really no clue here May 17, 2019 at 8:06
  • After removing the custom routes and adding the default route it suddenly worked. How can I fix this without removing the custom routes? May 17, 2019 at 8:21
  • the url paths follows your routes defined in route.config. So it goes right as per your routes.@Катерина
    – Ricky
    May 17, 2019 at 9:09
1

Your custom route (the second in your RegisterRoutes code above) seems to be incorrect... based on what you stated above, it can/should be this:

routes.MapRoute(
    "MyCustomRoute", // Route name
    "Login/Verify", // (No parameters needed)
    new { controller = "Login", action = "Verify" } // Parameter defaults
);

With this setup, alter the first line of your razor code to be this:

@using (Html.BeginRouteForm("MyCustomRoute", FormMethod.Post }))
{
    <fieldset class="clearfix">
    ...
}

Using Html.BeginRouteForm will automatically use the defaults specified in your custom route; no need to add the defaults in Razor. Using FormMethod.Post as the second parameter will render your form's method as a POST.

Edit: Let's fix your general Route problem. You're attempting to use Attribute Routing, which is described well here: https://www.dotnettricks.com/learn/mvc/understanding-attribute-routing-in-aspnet-mvc. I'd contend that it isn't necessary here.

First, fix your default Route (last route in your RegisterRoutes code) like so:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    // Change the 'controller' and 'action' parameters here to point to the Main page in your application.

Your default Route is very important, and should be configured as a catch-all for any requests can be simply mapped to a Controller/Action combination. I suspect that you experienced problems because your default Route was altered.

Next, comment out the Route and RoutePrefix attributes in your Login Controller... there's hardly a need for the [Route("Verify")] directive if you're using 'RegisterRoutes' properly.

// [RoutePrefix("Login")]
public class LoginController : Controller
{
    // GET: Login
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    // [Route("Verify")] //Matches GET login/verify
    public ActionResult Verify(string username, string password)
    {...}
}

Now that the default Route is set up properly, the url '/Login' should take you to your Login screen, because the default action is "Index" (it's the default in your default Route above.)

1
  • Thanks for the reply, however the error still remains: 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. May 27, 2019 at 7:02
0

Try adding routes.LowercaseUrls = true; to your route config as you seem to prefer lower-case versions of routes:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.LowercaseUrls = true;

            routes.MapRoute(
                name: "TrailersOverview",
                url: "{TrailersOverview}/{action}/{vendid}",
                defaults: new { controller = "TrailersOverview", action = "Index", vendId = UrlParameter.Optional }
            );

             routes.MapRoute(
                "MyCustomRoute", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                name: "Default",
                url: "{*}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

And for the default route, I think you wanted to mention {*} for url to match everything else?

3
  • Thanks for the reply, it didn't fix the error though. And yes about the * question :) May 19, 2019 at 19:47
  • Is the Login controller the only one that doesn't work or is it the same case for TrailersOverview?
    – Siavas
    May 19, 2019 at 19:48
  • Ok, what is the link you get when submitting the form?
    – Siavas
    May 21, 2019 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.