I have a newly created MVC 6 app, based on the ASP.NET 5 Web Application template, and I would like to include some Web API functionality in this project. It's easy to add an API/Controllers
folder, and add [api] controllers there, but I cannot configure Web API, When I try and add the following code:
我有一个基于ASP.NET 5 Web应用程序模板的新创建的MVC 6应用程序,我想在此项目中包含一些Web API功能。添加API / Controllers文件夹很容易,并在那里添加[api]控制器,但我无法配置Web API,当我尝试添加以下代码时:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I get a compile error that HttpConfiguration
is an unknown type. I have tried adding references to web API ( I can't recall which), but showed as errors in my references folder.
我得到一个编译错误,HttpConfiguration是一个未知类型。我已经尝试添加对Web API的引用(我不记得哪些),但在我的参考文件夹中显示为错误。
1 个解决方案
#1
0
Too big to comment, so putting it here:
太大了评论,所以把它放在这里:
If you create MVC6 application, you can just inherit your Api controller from Microsoft.AspNet.Mvc.Controller
and no need for System.Web.Http.ApiController
since mvc and webapi are getting under one roof.
如果您创建MVC6应用程序,您可以从Microsoft.AspNet.Mvc.Controller继承您的Api控制器,而不需要System.Web.Http.ApiController,因为mvc和webapi都在同一个屋檐下。
You can check more in Migrate Models and Controllers section
您可以在迁移模型和控制器部分中查看更多信息
Getting started: WebApi in MVC6
入门:MVC6中的WebApi
Edit
You can try following steps to resolve System.Web.Http.HttpConfiguration
您可以尝试以下步骤来解析System.Web.Http.HttpConfiguration
1) Add reference to following dependency in project.json
file (you can add as Nuget
package also)
1)在project.json文件中添加对以下依赖项的引用(也可以添加为Nuget包)
"dependencies": {
...
...
"Microsoft.AspNet.WebApi": "5.2.3"
},
2) Add reference to System.Web
from framework assemblies section That should do following change in "project.json" otherwise do it manually
2)从框架程序集部分添加对System.Web的引用应该在“project.json”中执行以下更改,否则请手动执行
"frameworks": {
"dnx451": {
"frameworkAssemblies": { //This section will be added
"System.Web": "4.0.0.0" //This reference will be added
}
}
3) Remove dnxcore50
under frameworks
section in project.json
, since System.Web
itself not available in DNX Core 5.0
. It is shown in intellisense also.
3)删除project.json中frameworks部分下的dnxcore50,因为System.Web本身在DNX Core 5.0中不可用。它也在intellisense中显示。
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.Web": "4.0.0.0"
}
},
"dnxcore50": { } //Remove this node. Remove comma(,) in above line too!!
},
4) Clean and Rebuild solution. Hopefully that should resolve the references.
4)清洁和重建解决方案。希望这应该解决参考。
I have followed these steps in a new MVC 6 application and was able to instantiate HttpConfiguration
properly just for test inside a controller.
我已经在新的MVC 6应用程序中执行了这些步骤,并且能够正确地实例化HttpConfiguration以便在控制器内进行测试。
Note: I didn't verified the functionality of the instance and so can't guaranty the functionality since its not in support!!
注意:我没有验证实例的功能,因此无法保证功能,因为它不支持!!
Hope this help you...
希望这能帮到你......
#1
0
Too big to comment, so putting it here:
太大了评论,所以把它放在这里:
If you create MVC6 application, you can just inherit your Api controller from Microsoft.AspNet.Mvc.Controller
and no need for System.Web.Http.ApiController
since mvc and webapi are getting under one roof.
如果您创建MVC6应用程序,您可以从Microsoft.AspNet.Mvc.Controller继承您的Api控制器,而不需要System.Web.Http.ApiController,因为mvc和webapi都在同一个屋檐下。
You can check more in Migrate Models and Controllers section
您可以在迁移模型和控制器部分中查看更多信息
Getting started: WebApi in MVC6
入门:MVC6中的WebApi
Edit
You can try following steps to resolve System.Web.Http.HttpConfiguration
您可以尝试以下步骤来解析System.Web.Http.HttpConfiguration
1) Add reference to following dependency in project.json
file (you can add as Nuget
package also)
1)在project.json文件中添加对以下依赖项的引用(也可以添加为Nuget包)
"dependencies": {
...
...
"Microsoft.AspNet.WebApi": "5.2.3"
},
2) Add reference to System.Web
from framework assemblies section That should do following change in "project.json" otherwise do it manually
2)从框架程序集部分添加对System.Web的引用应该在“project.json”中执行以下更改,否则请手动执行
"frameworks": {
"dnx451": {
"frameworkAssemblies": { //This section will be added
"System.Web": "4.0.0.0" //This reference will be added
}
}
3) Remove dnxcore50
under frameworks
section in project.json
, since System.Web
itself not available in DNX Core 5.0
. It is shown in intellisense also.
3)删除project.json中frameworks部分下的dnxcore50,因为System.Web本身在DNX Core 5.0中不可用。它也在intellisense中显示。
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.Web": "4.0.0.0"
}
},
"dnxcore50": { } //Remove this node. Remove comma(,) in above line too!!
},
4) Clean and Rebuild solution. Hopefully that should resolve the references.
4)清洁和重建解决方案。希望这应该解决参考。
I have followed these steps in a new MVC 6 application and was able to instantiate HttpConfiguration
properly just for test inside a controller.
我已经在新的MVC 6应用程序中执行了这些步骤,并且能够正确地实例化HttpConfiguration以便在控制器内进行测试。
Note: I didn't verified the functionality of the instance and so can't guaranty the functionality since its not in support!!
注意:我没有验证实例的功能,因此无法保证功能,因为它不支持!!
Hope this help you...
希望这能帮到你......