Changing Routes with Asp.Net MVC

Asked

Viewed 2,448 times

4

I’m trying to change a route in my project, the control is 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:

Server Error in Application '/'.

Unable to find resource.

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

Requested URL: /tags

Version Information: Microsoft . NET Framework Version:4.0.30319; Version of ASP.NET:4.0.30319.34248

Altering:

    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 view Index.cshtml inside the briefcase Tag inside the briefcase Views, I believe that it is not a problem that the view does not exist and the route is not working.

Another thing is that if you rotate with:

http://localhost:7071/Tag/Index

Also from the mistake that there is no /Tag/Index

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

I added the following code to Routeconfig, but it didn’t work:

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

I’d like to use the Route[("tags")] how monstrous this link

  • Just by looking, maybe I’ve seen the error... You declared this route you’re trying to access ? In Routesconfig same... And if you try to access the url: http://localhost:7071/Index, works ?

  • I put more information about the problem, the RouteConfig has not been changed, is an application of thing controls, I have not changed virtually anything of configuration.

  • So maybe that’s the problem... Asp.net mvc recognizes only the routes that are declared and configured in Routes... Try adding a new route in the Routes file, a copy and paste even, below the default route, if it works we put as a response.

  • I added another route, it didn’t work, I put more information in the question

  • And so [Route("/Tags/Index")] ?

  • It wasn’t, the impression is that the Route does not work.

  • Just try to comment on this default route and leave yours to watch...

Show 2 more comments

1 answer

2


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

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

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

In the end, the method RegisterRoutes 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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.