I'm new to MVC
, still learning ins and outs by converting .NET
App to MVC
.
I have trouble linking to another Action
in my controller
from the page inside of the "Area"
section.
I created Areas
and still have the default route set up.
The Area
has the following structure:
Area
- Admin
- Controller
UserController.cs
-Model
-Views
User
Index.cshtml
Index.cshtml
has a link that should call "Index"
action in AccountController
to open "Default"
view:
<div class="pagenavigator">@Html.ActionLink("Main Menu", "Index", new { area = "", controller="Account" })</div>
The default structure of the application is the following:
- Controller
AccountController.cs
- Views
- Account
Default.cshtml
Login.cshtml
My default structure has also controller folder that has a controller with Login (default) action set in RouteConfig.cs
:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
Here is Account Controller
:
public class AccountController : Controller
{
public ActionResult Login(string returnUrl)
{
StringBuilder sb = new StringBuilder();
sb.Append("Web Security Administration Portal<br/><br/>");
ViewBag.ReturnUrl = returnUrl;
ViewBag.Message = sb.ToString();
return View();
}
public ActionResult Index()
{
return View("Default");
}
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
bool authenticated = Security.AuthenticateLANUser(model.UserName, model.Password);
if (!authenticated)
{
Session["authenticated"] = false;
System.Text.StringBuilder errorMsg = new System.Text.StringBuilder();
errorMsg.Append("Invalid Login/Password entered.");
errorMsg.Append("We were not able to authenticate you in in Active Directory based on the information entered, ");
errorMsg.Append("but we recorded your attempt for audit purposes.");
ModelState.AddModelError("", errorMsg.ToString());
return View(model);
}
else
{
return View("Default");
}
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
}
What should I do if I need to link to "Default"
view defined under default application route, from Index.cshtml
defined under Area
, so when I click the link, my "AccountController"
gets called with the correct "Index"
action?
In short, I need to find out the way to link from "Area" section to a controller's action in default application section, so the another correct Action gets called, which is not specified in default route mapping Right now, the link is broken.
When I view the link in the source, I see the following: <a href="/Account/Index">
, but when I click on the link, I'm getting the error saying: The resource cannot be found with Requested URL: /login.aspx
Here is AdminAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}