2

My end goal is to have a <Script> line in the _layout that will be generated from Database data. Step 1 for me is to simply display a comment line in the "head" portion of the page. I have a test program that does this just fine. When I copy the code over to my site I get "No route in the route table matches the supplied values." when I run the web. The error is pointing to the @Html.Action("OVars","MyO") statement in _layout.

I have tried to use Glimpse and RouteDebugger but I am not able to get any data on what this route actually is being generated as and then why it is failing. The output from RouteDebugger has a list of about 30 to 40 routes of which only the "Default" route is in Global.asax.cs. So I can only assume that these routes are being created elsewhere in the code.

@Html.Action("OVars","MyO")

The controller contains:

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

namespace MySite.Controllers
{
    public class MyOController : Controller
    {
        // ChildActionOnly will not allow direct displaying of this page
        [ChildActionOnly]
        public ActionResult OVars()
        {
            return PartialView("_OVars");
        }
    }
}

The _OVars partialview:

   <!-- Test comment simply for displaying -->

Global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine());

    routes.IgnoreRoute("{resource}.ico");
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults                
    );
}

Any suggestions on what to look at would be a help. For all I know there is a parameter set that I am not aware of that needs to be changed.

Thanks

John G
  • 59
  • 6

2 Answers2

3

Solved (but I do not understand "Areas, something more to learn").

I changed the line @Html.Action("OVars","MyO") to @Html.Action("OVars","MyO", new { area = string.Empty }).

I kept making code changes until I received an error message that when I looked it up found some text similar to "This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to.". The example presented was to add , new { area = string.Empty } to the @Html.Action statement.

Now the @Html.Action is working correctly and I can move forward with having the controller access the database for information.

Next up on the learning agenda is "Understanding Areas".

John G
  • 59
  • 6
  • Thanks, I was providing the name of the area as a string and banging my head of the desk trying to work out why it wasn't working :) – JMK Aug 29 '14 at 10:21
0

In case someone has the same struggle as me. I had a custom route:

        routes.MapRoute(
            name: "MyPrefixRoute",
            url: "myprefix/{code}/{name}",
            defaults: new { controller = "Account", action = "MyPrefix", name = UrlParameter.Optional }
        );

And then my default route (which is a slight variation of the MVC default route):

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

The exception was only raised when browsing /myprefix/123/the-name and not /myprefix/123?name=the-name nor /myprefix/123.

The only thing that fixed it was to add a third custom route without UrlParameter.Optional:

        routes.MapRoute(
            name: "DefaultAction",
            url: "{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" }
        );

Although it works now, I would be happy to get an explanation ;-)

webStuff
  • 1,468
  • 14
  • 22