Deleted bin folder and also have cleaned and rebuilt multiple times. For some reason after checking all the required files where routes get configured I am running into the naming error, I changed it and manuplated it to my best extent, but I am stuck. I am trying to learn asp.net, but this error has me dumbfounded.
This is folder system, I have expanded the important ones:
Project-
Reference-
Packages-
App_Start-
-RouteConfig.cs
-WebApiConfig.cs
Areas-
-Billing
-Invoice
Content-
Controllers-
Models-
Scripts-
Views-
Global.asax
Pacages.config
Web.config
I have a registration file in each of my Areas.
Here is the registration code form one of the Areas.
using System;
using System.Web.Mvc;
namespace WORK_GODDAMN.Areas.Billing
{
public class BillingAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Billing";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Billing_default",
"Billing/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
Here is my Global.asax code
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
namespace WORK_GODDAMN
{
public class Global : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
Here is my routeConfig code
using System.Web.Mvc;
using System.Web.Routing;
namespace WORK_GODDAMN
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.Controllers" }
);
}
}
}
cshtml code for the Index Page which should redirect to the predefined Areas.
<!DOCTYPE html>
<html>
@using System.Web.Optimization
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Scripts.Render("~/Scripts/modernizr.js")
</head>
<body>
<div>
<div>
<h2>{Demo}- Pluggable Modules</h2>
</div>
<div id="nav">
@Html.ActionLink("Home","Index","Home", new {Area=""}, null) |
@Html.ActionLink("Marketing","Index","Marketing", new {Area="Marketing"}, null) |
@Html.ActionLink("BillingMain", "Index", new { Area = "Billing" })
</div>
<hr />
<div>
@RenderBody()
</div>
</div>
@Scripts.Render("~/Scripts/jquery.js")
@RenderSection("Scripts", required: false)
</body>
</html>
I am not why this confliction is happening, I have cleaned and rebuilt multiple times, I am using 2017 Mac Visual Studio so I had to configure Areas myself.
In this ActionLink:
@Html.ActionLink("BillingMain", "Index", new { Area = "Billing" })
You are not specifying a controller argument.
In your Area registration, you are also not specifying a controller argument.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Billing_default",
"Billing/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
If you don't specify a controller argument in your route, it is a required argument, not an optional one. As a result, this action link will not match your area route and will try to match the next route registered (most likely your default route).
To make the controller optional, you must specify a default.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Billing_default",
"Billing/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Otherwise, you must supply it in your ActionLink for it to match:
@Html.ActionLink("Billing","Index","BillingMain", new {Area=""}, null)
The most logical behavior for route values is usually to make the values required (like you have) so they only match if the value is provided by the ActionLink.