1.
I have a home page (Home / Index). Here you select the language.
Here the url is this: "localhost:xxxx".
2.
After selecting the language, the following is a login page (Account / Index)
Here the url is this: "localhost:xxxx/Account/Index?language=en-US".
3. When entering data (Username / Password) and click on the Logon button, redirects to User / Index but the url stays in Account/LogOn
My Form:
<% using (Html.BeginForm("LogOn", "Account")) { %>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="username">Username:</label>
<%: Html.TextBoxFor(m => m.Username, new { placeholder = "Username" })%>
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="password">Password:</label>
<%: Html.PasswordFor(m => m.Password, new { placeholder = "Password" })%>
</div>
<fieldset class="ui-grid-a">
<div class="ui-block-a"><button type="reset" data-theme="d">Reset</button></div>
<div class="ui-block-b"><button type="submit" data-theme="b">Log On</button></div>
</fieldset>
<% } %>
Account Controller:
[HandleError]
public class AccountController : Controller
{
public ActionResult Index(string language = "es-Es")
{
return View();
}
[HttpPost]
public ActionResult LogOn(UserModel user)
{
FormsAuthentication.SetAuthCookie(user.Username, false);
return RedirectToAction("Index", "User");
}
public ActionResult LogOff()
{
return View();
}
}
Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
How to make the url is: localhost:xxxx/User/Index ?
In your LogOn use permanent redirect:
[HttpPost]
public ActionResult LogOn(UserModel user)
{
FormsAuthentication.SetAuthCookie(user.Username, false);
return RedirectToActionPermanent("Index", "User");
}
In your Account/Index.cshtml
view replace:
@using (Html.BeginForm())
{
...
}
with:
@using (Html.BeginForm("LogOn", "Account"))
{
...
}
so that you invoke the LogOn
action on the Account controller when you submit the form and not the Index action (which simply returns the same view).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With