问题描述
我尝试使用基于属性的路由.但是,当我尝试以下代码片段激活基于属性的路由时,出现以下错误消息:
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:
这是我的代码:
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"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!