user1716097
user1716097

Reputation: 9

mvc5 attribute based routing not working

I am using visual studio 2013 and created a default mvc project.In home controller I have modified the below

 [RoutePrefix("home")]
    [Route("{action=index}")]
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        //[Route("Home/About")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

In RouteConfig.cs I have done the following modification

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();
            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            //);
        }
    }

This is not working.But when I go for convention based routing it is working. Plz help me.

Upvotes: 0

Views: 3438

Answers (1)

Charlie
Charlie

Reputation: 177

to make a web api create a WebApiConfig.cs :

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
        }
    }
}

in your global.asax :

GlobalConfiguration.Configure(WebApiConfig.Register);

and then you can define controller :

[RoutePrefix("home")]
    public class HomeController : ApiController
    {
        [Route("action")]
        public string myaction()
        {
            return "hello world";
        }
    }

this way the route /home/action will send back the defined string now the big difference between WebApi and MVC is that you dont use your server side to Template and provide views, it is mainly used for REST actions: GET PUT POST DELETE I don't know what you want to achieve but I hope this will be usefull

Edit: I was thinking maybe you forgot to register your MVC routes in global.asax there should be a similar call to make like:

GlobalConfiguration.Configure(RegisterRoutes.routes);

something like that and in your code try to change :

[RoutePrefix("Home")]
    public class HomeController : Controller
    {
        [Route("Index")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("About")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

and routes should be /Home/Index and /Home/About

Upvotes: 1

Related Questions