1

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 }
        );
    }
gene
  • 2,098
  • 7
  • 40
  • 98
  • 1
    You can specify the area in the ActionLink as well: http://stackoverflow.com/a/2036325/1373170 Also in that other question you will find how to link to something not in an area, by using `area = ""` – Pablo Romeo May 11 '15 at 18:11
  • I need to call another action in that controller as well. Currently it gets defaulted to the one specified in `RouteConfig.cs` – gene May 11 '15 at 18:32
  • Are you sure you are using the correct override of ActionLink? You might need to add the `new {}` as mentioned in the comments of that Answer. – Pablo Romeo May 11 '15 at 18:36
  • Yes, I did. Also, the link shows the correct path, but when clicking on it, it is looking for another action. Please refer to the modified question. I think, it is looking for the default route, specified in `RouteConfig.cs` – gene May 11 '15 at 18:40
  • Have you set up the route for your admin area as well? http://stackoverflow.com/a/15616483/1373170 Also, if possible, try to add the full stacktrace error info, that might shed some light on the problem. Also, the fact that it mentions `login.aspx` is a bit odd. Are you sure you don't have that defined in the forms authentication section of your web.config as the loginUrl? – Pablo Romeo May 11 '15 at 18:48
  • The error I get, just regular `HTTP 404` error saying that resource cannot be found and that `Requested URL: /login.aspx`. Route for admin is set up by default and I did not change anything there. It points to the correct action and finds the right controller. The only problem is when I click on the link, it cannot find the action specified for the default controller – gene May 11 '15 at 19:06
  • I added `AdminAreaRegistration` to my question – gene May 11 '15 at 19:13
  • @Pablo Romero, Looks like application does nor point to the correct route, but it seems to me that everything is specified the right way – gene May 11 '15 at 22:03
  • Something seems yo be issuing a weird redirect to login.aspx. Are sure you aren't using forms authentication and have URL specified in the web.config? – Pablo Romeo May 11 '15 at 22:06
  • My `AccountController` has `[Authorize] and [InitializeSimpleMembership]` `attributes` and login methods have `[AllowAnonymous]` and `[HttpPost]` attributes, but `Index` method does not have anything. My `web.config` does not have anything related to `loginUrl` as well. The line ` – gene May 11 '15 at 22:28

0 Answers0