"RouteCollection"不包含"MapMvcAttributeRoutes"的定义 [英] 'RouteCollection' doesn't contain a definition for 'MapMvcAttributeRoutes'

查看:139
本文介绍了"RouteCollection"不包含"MapMvcAttributeRoutes"的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用基于属性的路由.但是,当我尝试以下代码片段激活基于属性的路由时,出现以下错误消息:

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'不包含以下定义 'MapMvcAttributeRoutes

RouteCollection' doesn't contain a definition for 'MapMvcAttributeRoutes

这是我的代码:

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

}

这些是参考文献:

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

我的项目的框架版本为 4.5.2 .

Thre Framework version for my project is 4.5.2.

这是怎么了?我该怎么解决?

Whats wrong here and how i fix this?

推荐答案

我现在可以正常使用了.我做了以下事情:

I got it work now. I did the following:

我首先添加了一个名为App_Start的新文件夹.在此文件夹中,我创建了一个名为RouteConfig.cs的新类文件.

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
    }

我在Application_Start方法中使用以下代码向我的项目添加了global.asax文件:

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);
    }

然后在Extras> NuGet Package Manager> Pacakge Manager Console下,添加了 Microsoft ASP. NET MVC 5.2.3 打包到我的项目中.

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

最后,我用以下代码添加了一个新的MVC5空控制器:

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

这篇关于"RouteCollection"不包含"MapMvcAttributeRoutes"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆