Changing Routes with Asp.Net MVC

4

I'm trying to change a route in my project, the control looks like this:

 public class TagController : Controller
    {
        // GET: Tag

        private MYEntities db = new MYEntities ();

        [Route("tags")]
        [OutputCache(Duration = 36000, VaryByParam = "none")]
        public ActionResult Index()
        {
            var t = db.TAGs.OrderBy(p => p.DESCRIPTION);

            return View(t.ToList());
        }
    }

When I run the URL:

http://localhost:7071/tags

It returns the error:

  

Application Server Error '/'.

     

Can not find resource.

     

Description: HTTP 404. The resource you are looking for (or one of   its dependencies) could not be removed, its name changed, or   is temporarily unavailable. Please review the URL and make sure   which is typed correctly.

     

Requested URL: / tags

     

Version Information: Microsoft .NET Framework Version: 4.0.30319;   ASP.NET Version: 04.0.30319.34248

Change:

    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 = "Categoria", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

There is a Index.cshtml view inside the Tag folder inside the Views folder, I believe it is not a view problem that does not exist but the route is not working.

Another thing to do is to run with:

http://localhost:7071/Tag/Index

Also the error saying that /Tag/Index does not exist

By taking note [Route("tags")] of method Index it works with the URL above.

I added the following code to RouteConfig, but it did not work:

            routes.MapRoute(
                name: "teste",
                url: "tags",
                defaults: new { controller = "Tag", action = "Index", id = UrlParameter.Optional }
            );

I would like to use Route[("tags")] as shown in link

    
asked by anonymous 30.05.2015 / 03:36

1 answer

2
To use attribute based routing, you need to call the MapHttpAttributeRoutes (for Web API 2) or MapMvcAttributeRoutes (for MVC) method:

 config.routes.MapMvcAttributeRoutes();
 config.MapHttpAttributeRoutes();

This will inspect the attributes in all controllers / actions and configure their routes.

At the end, the RegisterRoutes method should look like this:

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

        // Attribute routing.     
        routes.MapMvcAttributeRoutes();

        // Convention-based routing.
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    }
}

//WebApiConfig.cs
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

More information:

30.05.2015 / 12:12