0

I'm trying to use route attributes in MVC 5. I created an empty MVC project in Visual Studio to experiment with and so far I can't get the routing to work. I'm using this page as a reference. I have the latest assemblies and updated all NuGet packages to their latest versions.

Here's my code:

// RouteConfig.cs
namespace MvcApplication1
{
    using System.Web.Mvc;
    using System.Web.Routing;

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // Enables MVC attribute routing.
            routes.MapMvcAttributeRoutes();

            // The default route mapping. These are "out of the bag" defaults.
            routes.MapRoute(null, "{controller}/{action}/{id}", new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });
        }
    }
}

// TestControler.cs
namespace MvcApplication1.Controllers
{
    using System.Web.Mvc;

    public class TestController : Controller
    {
        public ContentResult Output1()
        {
            return Content("Output 1");
        }

        [Route("Test/Output2")]
        public ContentResult Test2()
        {
            return Content("Output 2");
        }
    }
}


@* Index.cshtml *@
@Html.Action("Output1", "Test")
@Html.Action("Output2", "Test")

The Output1() method renders properly. However, when Output2() is rendered, I get the error "A public action method 'Output2' was not found on controller 'MvcApplication1.Controllers.TestController'."

2 Answers 2

8

Your action is named Test2 not Output2. Change the following

@Html.Action("Test2", "Test")
Sign up to request clarification or add additional context in comments.

2 Comments

That is correct. I named it Test2 and would like the route attribute to intercept the call to "/test/output2" and use the Test2 method. In the examples on the blog post I linked to, they had route attributes that were named differently than the method names. I'd like to be able to do this.
You can name the routes anything you want, but the Html.Action method parameters are based on the actual method names in the controller.
1

This is because @Html.Action will not actually use routing. With it you explicitly specify actions and controllers. The routing will be used when someone for instance makes the http://example.org/Test/Output2 request from a browser.

2 Comments

This is very bad news for my situation. In my project, I have some MVC routing which isn't being honored and I have to use relative URLs instead of Url.Action. This is problematic because I use ReSharper to check for all uses of a particular controller method in my views. With the relative Urls, I'm unable to find all uses of a particular controller method and I have to do a project-wide string search. I was hoping the route attributes would solve this problem, but it looks like they are more suited for Web API than MVC.
@Halcyon If you have your routing setup correctly, the Action() method will output the correct route, even if you use attributes.

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.