1

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.

  • Did you try moving `AreaRegistration.RegisterAllAreas();` to top of the registrations section in `Application_Start()` inside `Global.asax` ? – Siva Gopal Jun 29 '17 at 19:22
  • Yes I did, I tired all sorts of combinations. – ZeroDay Fracture Jun 29 '17 at 19:27
  • 1
    Also you need to remove `AreaRegistration.RegisterAllAreas();` from `RouteConfig` class which will try to re-register areas and would result in the kind of error you are getting. – Siva Gopal Jun 29 '17 at 19:29
  • If I do that then I get a not found 404 even after mapping the link to a button to take me there, so the Areas is not getting registered on start. – ZeroDay Fracture Jun 29 '17 at 19:31
  • Ill add my html code, I might be missing something simple – ZeroDay Fracture Jun 29 '17 at 19:31
  • Try changing the order of `AreaRegistration.RegisterAllAreas();` to top in `Application_Start()` and remove it from `RouteConfig` class and check. I guess it should work. – Siva Gopal Jun 29 '17 at 19:34
  • I did and am still getting 404 not-found error, even though the actionLink specifically points there – ZeroDay Fracture Jun 29 '17 at 19:35
  • Can you go one more time what I said in my previous comment pls? "Try changing the order of `AreaRegistration.RegisterAllAreas();` to top in `Application_Start()` and remove it from `RouteConfig` class and check". But your updated code doesn't show what I said, instead you moved `AreaRegistration.RegisterAllAreas(); ` in the `RouteConfig` itself to top and that is not what I said. – Siva Gopal Jun 29 '17 at 19:39
  • Done and still getting the same issue. I actually had it like that from the beginning and changed it to see if it was working. – ZeroDay Fracture Jun 29 '17 at 19:42
  • Did you copy and paste the code from the `BillingAreaRreistration` to the `InvoiceAreaRegistration`? There could be a name duplication in one of them. – jamiedanq Jun 29 '17 at 21:15
  • I completely removed Invoice, now it just goes up a list and tells me Billing is not unique. Now Im getting not found 404 – ZeroDay Fracture Jun 29 '17 at 21:22

1 Answers1

1

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • But this is searching in Views folder not in Areas/Billing/Views. I did have the controller defined, but took it out while trying different things out. – ZeroDay Fracture Jun 30 '17 at 16:02
  • I have also added namespace to point to the right folder, still getting the same error where it says `"The view 'Billing' or its master was not found or no view engine supports the searched locations. The following locations were searched: ......"` – ZeroDay Fracture Jun 30 '17 at 16:13
  • Adding the namespace is not enough to make MVC look for views in another location. If you break MVC's view location conventions, you either have to [specify view locations explicitly](https://stackoverflow.com/a/23335609/181087) or [modify the search locations in the `ViewEngine`](https://stackoverflow.com/a/909594/181087) in order for that to happen. Still, that was not your question - this fixes the routing problem you were having. – NightOwl888 Jun 30 '17 at 16:21
  • It is not searching in the Areas? What URL are you using to try to access it? You should be using `/Billing/BillingMain/Index` (assuming `BillingMainController` is your controller name and `Index` is your action name, but that isn't clear from your question either), and it will reach your area route and then search for your view in the right location. – NightOwl888 Jun 30 '17 at 16:30
  • Thanks for your help, Im very new to the web dev scene. And I am accessing it through localhost:8080/BillingMain, which gives me the error. Yes BillingMainController is the name of my Billing controller. – ZeroDay Fracture Jun 30 '17 at 17:04