0

Ok, so this one is sending me a little "loopy". I know I'm new to MVC but blimey this is a pain.

Basically all I'm trying to do is set "override'able" action methods in my Controller using RouteAttrbitues which in turns throws the 403.

I'm hoping to get pushed in the right direction. Thanks in advance!

Config

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapMvcAttributeRoutes();
  routes.RouteExistingFiles = true;
}

Controller

[RoutePrefix("CMSPage")]
    public class CMSPageController : Controller
    {
        private IEntityRepository<Type> _entityRepo;

        [HttpGet]
        [Route("", Name = "Home")]
        public ActionResult Index()
        {
            //var model = entityRepo.FindByName("Home");
            return Index();
        }
        [HttpGet]
        [Route("{topLevelPageName}", Name = "TopLevelPage")]
        public ActionResult Index(string topLevelPageName)
        {
            //var model = entityRepo.FindByName(name);
            return Index(topLevelPageName);
        }
        [HttpGet]
        [Route("{topLevelPageName}/{pageName}", Name = "PageName")]
        public ActionResult Index(string topLevelPageName, string pageName)
        {
            //var model = entityRepo.FindByName(name);
            return Index(topLevelPageName, pageName);
        }

    }

This code is pretty basic and I'm almost fairly certain I'm missing something silly.

A nudge in the right direction will be of great help!

Regards,

Tez Wingfield
  • 2,129
  • 5
  • 26
  • 46

1 Answers1

0

You are using the same method name for multiple httpget functions, this isnt MVC functionality as there is no way for the routes to tell if you wanted a null value, or if you wanted to go to a different method altogether.

To fix this, rename the 2nd and 3rd index method, but keep the routing the same. this should fix the controllers ability to resolve which method to pass your url to.

LiamHT
  • 1,312
  • 3
  • 12
  • 28
  • Cheers, yes that makes sense. I did originally have that but liked the idea/feel of overrides, in which I took from this SO post: http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Tez Wingfield Sep 09 '16 at 12:22
  • no worries.... Your returns statements are also recursive.. dont know if that is by design. – LiamHT Sep 09 '16 at 12:23
  • Sorry poor MVC knowledge - recursive in what way? – Tez Wingfield Sep 09 '16 at 12:25
  • Your index method is returning index, so it will just call itself until it throws an exception. If you are trying to return the view then you need to `return View("Index")` – LiamHT Sep 09 '16 at 12:27
  • Ahhh yes, that was intentional. I thought the same but the other SO post had a similar process. – Tez Wingfield Sep 09 '16 at 12:28
  • as an aside, its often easier to just have {controller}/{action}/{id} as a default route in a routeconfig and explicitly declare anything else that is needed. with Index as a default then MVC routing normally acts as you would expect with that rule. MyWebsite.com/Hello would go to the index method of the hello controller Mywebsite.com/Hello/World would go to the world method of the hello controller Mywebsite.com/Hello/world/1 would go to the world method and pass a variable of 1 into the Id field expected – LiamHT Sep 09 '16 at 12:37
  • Cheers! Because the system plugs into an api, I needed to be rather strict with the routes and the ability to use one Controller for rendering of the page. I have now achieved what I wanted to do by keeping one index method with optional params and then stripping out Controller/Action on the route: url: "{topLevelPageName}/{pageName}". Thank you for your time! – Tez Wingfield Sep 09 '16 at 12:46