本文介绍了将 Web API 添加到 asp.net MVC 项目的问题(追溯)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2013 中使用 asp.net mvc 迈出第一步,但遇到了一个奇怪的问题.

I'm taking my first steps with asp.net mvc in Visual Studio 2013 and I've run into a weird problem.

我以 MVC 的形式开始了我的项目,并且没有选中Web API"框以包含核心引用.但是现在我想使用 Web API 功能.我创建了一个新的somethingController"控制器,选择了 API 控制器(空),Visual Studio 做了它的魔法,创建了一个路由文件等.

I started my Project as MVC and did NOT check the "Web API" box to include the core references. However now I want to use the Web API functionality. I created a new "somethingController" controller, selected API controller (empty) and Visual Studio did it's magic, creating a routing file, etc.

但是,当尝试访问 URL/api/something/时,我得到了 404.我相信路由不起作用或者还有一些我还没有考虑过的其他问题......

However when trying to access the URL /api/something/ I geta 404. I believe the routing doesn't work or there is some other problem I haven'T thought about yet...

如果我创建一个新项目并选中Web API"框,它就可以正常工作.但是,如果您有一个更大的项目并且想要添加 Web API 功能,那么必须有一种简单的方法来可靠地将其添加到项目中,对吗?

It works just fine if I create a new Project and checkt the "Web API" box. But if you have a larger project and want to add Web API functionality there must be an easy way to reliable add it to the project, right?

谁能解释一下

a) 问题出在哪里(路由?缺少引用?其他问题?)

a) Where the problem lies (routing? Missing references? something else?)

b) 如何可靠地将 Web API 内容添加到未选中复选框的项目中.

b) How can one reliably add the Web API stuff to a project that wasn't started with the checkbox checked.

非常感谢.

推荐答案

好的,看来我已经成功了.当您将第一个 API 控制器添加到未检查 Web API 的项目时,会弹出一个自述文件,其中包含一些附加信息:

Ok, seems I got it working. When you add your first API controller to a project that does not have the Web API checked, a readme file pops up with some additional information:

我尝试添加

WebApiConfig.Register(GlobalConfiguration.Configuration);

到 global.asax 并且它不起作用.自述文件显示以下代码

to the global.asax and it did not work. the readme show the following code

GlobalConfiguration.Configure(WebApiConfig.Register);

那行得通,虽然(在我看来)两条线做的完全一样??

that DOES work, although it seems (to me) that both lines do exactly the same thing??

这篇关于将 Web API 添加到 asp.net MVC 项目的问题(追溯)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:38