'RouteCollection' doesn't contain a definition for 'MapMvcAttributeRoutes'

余生颓废 提交于 2020-01-05 05:25:07

问题


I try to use attribute based routing. But when i try the following snippet to activate attribute based routing i get the following error message:

RouteCollection' doesn't contain a definition for 'MapMvcAttributeRoutes

This is my code:

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

}

These are the references:

using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

Thre Framework version for my project is 4.5.2.

Whats wrong here and how i fix this?


回答1:


I got it work now. I did the following:

I added first a new folder called App_Start. Within this folder i created a new class file named RouteConfig.cs.

The following code was added to the file above:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes(); //Attribute Routing
    }

The i added a global.asax file to my project with the following code in Application_Start method:

    protected void Application_Start(object sender, EventArgs e)
    {
        //AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

Then under Extras > NuGet Package Manager > Pacakge Manager Console i added the Microsoft ASP.NET MVC 5.2.3 package to my project.

Finally i added a new MVC5 Empty controller with following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication4.Controllers
{
    [RoutePrefix("products")]
    public class DefaultController : Controller
    {
        // GET: Default
        [Route]
        public ActionResult Index()
        {
            return Content("It Works!");

            //return View();
        }
    }
}

After this i was able to open my project in the project by typing:

http://localhost:65311/products


来源:https://stackoverflow.com/questions/38850322/routecollection-doesnt-contain-a-definition-for-mapmvcattributeroutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!