parismiguel parismiguel - 2 months ago 15
ASP.NET (C#) Question

ASP.net MVC 5 Areas in existing project not working

I've been trying to set up a new Area in my current project following details instructions but I'm not able to get a successful result.

Could somebody help me, please? Here is my code so far:

RouteConfig.cs

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

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

routes.MapRoute(
name: "Main_default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional },
namespaces: new string[] { "MesaServicio.Controllers" }
);


routes.MapRoute(
name: "signin-google",
url: "signin-google",
defaults: new
{
controller = "Account",
action = "ExternalLoginCallback"
},
namespaces: new string[] { "MesaServicio.Controllers" }
);
}
}
}


AdminAreaRegistration.cs

using System.Web.Mvc;

namespace MesaServicio.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 },
namespaces: new[] { "MesaServicio.Areas.Admin.Controllers" }
);
}
}
}


AdminController.cs

using System.Linq;
using System.Web.Mvc;
using MesaServicio.Models;

namespace MesaServicio.Areas.Admin.Controllers
{
public class AdminController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();

// GET: Admin/Admin
public ActionResult Prueba()
{
return View(db.Corps.ToList());
}
}
}


Index.cshtml

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>


Area files structure screenshot

File structure

Global.asax.cs

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MesaServicio
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}


Link trigger

<li><a href="@Url.Action("Index","Admin", new {area = "admin" })"><i class="fa fa-circle-o"></i> Admin</a></li>


RESULT


https://localhost:44306/admin/

Server Error in '/' Application.

No parameterless constructor defined for this object.

Answer

AdminControllerdoesn't have an Index action, it instead has a Prueba action. Try renaming method Prueba to Index.

Comments