0

I've updated my code to use Area as suggested but the problem still exist. /dashboard is still available.

My Controllers folder has HomeController and AccountController. I have Areas/Admin/Controllers/DashboardController.cs

Problem:

  1. My area admin controller can be accessed like this /admin/dashboard, but the problem is it can also be accessed using /dashboard -> this should show 404

here is my RouteConfig:

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

AdminAreaRegistration:

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

        }
2
  • project.controllers.admin namespace is within project.controllers namespace try changing the name space for admin to one that is not a child of project.controllers. something like project.admin and see if that works Commented Nov 11, 2016 at 16:16
  • 1
    This sounds like a good time to perhaps look at Areas.. Commented Nov 11, 2016 at 16:20

2 Answers 2

4

The /dashboard call is routed by the Default routing rule.

You can make the Default not to process the calls made to the dashboard controller by adding a constraint.

For example:

In the default routing rule you can add a constraint like the following:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "project.Controllers" },
    constraints: new { controller = new Constraints.IsNotDashboard() }
);

Then, you can declare the constraint like this:

using System.Web;
using System.Web.Routing;

public class IsNotDashboard : IRouteConstraint
{
    public IsNotDashboard()
    {
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        string controller = values["controller"].ToString().ToLower();

        return controller != "dashboard";
    }
}

With this constraint, all calls that match the dashboard controller will not be processed by the Default routing rule.

Sign up to request clarification or add additional context in comments.

4 Comments

the constraints work, but it's very tedious to add additional constraints when i create more admin area controllers as they will also be routed to default right?
@HeeroYuy you could have a single constraint for all new controllers. Another way is to check the httpContext.Request.Url.LocalPath property, and if it starts with /admin or not. If that seems a bit hacky to you, consider using Areas where they have this behavior by default.
@HeeroYuy I noticed you changed your question and you now are using areas. Are you still having the issue? Perhaps you have two Dashboard controllers in your project? and one of them is outside the area's namespace
I've deleted the Dashboard controller outside Area. But i've already found a fix to my problem. I posted below. Thanks :)
1

Thanks guys.

After searching the net, I finally found what works best for my problem.

The problem was that all controllers are being handled as well in Default route, so I just added controller constraints to Default. This way Default route will only accept request on specified controllers. Below is my new RouteConfig

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { controller = @"(Account|Manage|Home)" }
            );

1 Comment

You added equal check in constraints. Can we add not-equal check in constraints like; constraints: new { controller != @"(Account|Manage|Home)" } ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.