0

I have created an "admin" area within my asp.net mvc 4 project. For now this area has just the basic's. Ideally, once the application runs, I want to the user to type /Admin in the URL and be re-directed to the Admin Index view. However when I run the program I get an error stating that "Admin cannot be found" ?? Am I missing a reference somewhere ??

Also, a regular user login page is the first view rendered, I need to bypass that login page to get to the Admin section. However I believe the following line is preventing a second log in option.

 WebSecurity.InitializeDatabaseConnection("Portal.Model.PortalDBContext","PatientPortalAccount", "PatientID", "Username", autoCreateTables: true);

Please keep in mind I cannot alter the regular user tables to include roles. If you haven't noticed already I'm still learning, so apologies for the long winded question...

admincontoller,

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;

namespace Portal.Web.Areas.Admin.Controllers
 {
public class AdminController : Controller
{
    //
    // GET: /Admin/Admin/

    public ActionResult Index()
    {
        return View();
    }

with an index actionResult, which has a view.

 @model Portal.Model.AdminDetails
 @{
ViewBag.Title = "Admin";
Layout = "~/Views/Shared/Login.cshtml";
}

This area also has the generated .cs file AdminAreaRegistration

  using System.Web.Mvc;

  namespace Portal.Web.Areas.Admin
  {
  public class AdminAreaRegistration : AreaRegistration
  {
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional },
            new [] { "Admin.Controllers" }
        );
    }
}
}

And within the Global.asax file....

  using System;
  using System.Linq;
  using System.Web.Http;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  using WebMatrix.WebData;

  namespace Portal.Web
  {
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
// visit http://go.microsoft.com/?LinkId=9394801

  public class MvcApplication : System.Web.HttpApplication
  {
    protected void Application_Start()
    {
        WebSecurity.InitializeDatabaseConnection("Portal.Model.PortalDBContext",             "PatientPortalAccount", "PatientID", "Username", autoCreateTables: true);

        //BOC - Remove Web Forms View Engine
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new RazorViewEngine());

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}
}
mkell
  • 681
  • 2
  • 10
  • 30

1 Answers1

1

You have a namespace constraint in your route: "Admin.Controllers" but your controller is in namespace "Portal.Web.Areas.Admin.Controllers"

Gregoire
  • 22,211
  • 6
  • 42
  • 70
  • Great, that did the trick, thank you. Any tips for finding away of getting to the Admin Index view without having to log in ? Am I right in saying the "WebSecurity" tab in Global.asax is preventing such an action ?? Please Understand I cannot make a regular user an administrator, as the regular user tables are sync'd with another product. – mkell Jun 27 '13 at 10:33
  • @mkell add [Auhorize] attribute on your controller to force a redirection to the login page – Gregoire Jun 27 '13 at 11:35
  • I added it above public class AdminController : Controller, I'm afraid it made no difference. a regular user still has to login before I can access the admin Index view via URL. – mkell Jun 27 '13 at 11:46
  • @mkell OK I has not understood your request and I am still not sure. However if you do not want authentication on a specifi action you can add [AllowAnonymous] attribute on the action. – Gregoire Jun 27 '13 at 12:21
  • I'm probably not explaining correctly. when the program run's the user is presented with a login screen. This screen is for the regular users. I need the administrator to be able to type "/Admin" in the URL in order to be re-directed to the adminLogin screen. A regular user cannot be an administrator and I cannot alter the regular users tables. Right now the only way to re-direct to the adminlogin is to have a regular user log on and then type "/Admin" into the URL...? – mkell Jun 27 '13 at 14:11
  • Ok. See: http://stackoverflow.com/questions/748287/how-to-override-change-formsauthentication-loginurl-in-certain-cases – Gregoire Jun 27 '13 at 14:15