JOb801
JOb801

Reputation: 5

Route Attribute Not Working

Trying to use a [Route] attribute in my MVC app, per this article: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

[Route("Error/{type?}")]
    public ActionResult Error(string type)
    {
        if (type == "Duplicate")
        {
            ViewBag.ErrorDetails = "There is already a redirect using that Vanity URL and Domain.";
        }

        if (type == "Unknown")
        {
            ViewBag.ErrorDetails = "An unknown error has occured. Probably your fault too.";
        }

        return View();
    }

I have MapMvcAttributeRoutes in RouteConfig.cs

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

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

        routes.MapMvcAttributeRoutes();
    }
}

I get a 404 when browsing to /Admin/Error/Duplicate but /Admin/Error?type=Duplicate works fine.

In addition to [Route("Error/{type}")] I have tried [Route("Admin/Error/{type})] and [Route("{type})] but no luck. I can't figure out where I'm going wrong.

Upvotes: 0

Views: 357

Answers (0)

Related Questions