asp.net-mvc 为什么无法导航到 web api 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25484176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't navigate to web api controller
提问by 1110
I use Web API for the first time to connect my mobile app to Web API.
I have MVC 5 project and I have created API folder and inside I have created ProductsController.
我第一次使用 Web API 将我的移动应用程序连接到 Web API。
我有 MVC 5 项目,我创建了 API 文件夹,在里面我创建了ProductsController.
namespace DDD.WebUI.API
{
public class ProductsController : ApiController
{
public string GetAllProducts()
{
return "Hello From WebAPI";
}
}
}
And I have tried to access this method from browser:
我尝试从浏览器访问此方法:
http://localhost:21879/DDD.WebUI/API/products/GetAllProducts
But I get 404.
It's probably something stupid but can't figure it :(
UPDATE
但我得到 404。
这可能是愚蠢的,但无法理解:(
更新
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultApi",
url: "API/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
UPDATE 2
I have a little progress:
I have update Application_Start()with:
更新 2
我有一点进步:我有更新Application_Start():
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
In WebApiConfigI have:
在WebApiConfig我有:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I still store API controller in Project/Api/ProductsController But now I can access it but still can't get value from it:
我仍然将 API 控制器存储在 Project/Api/ProductsController 但现在我可以访问它但仍然无法从中获取价值:
http://localhost:21879/api/products/getallproducts
But error I get is:
但我得到的错误是:
<Error>
<Message>The request is invalid.</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'GSL.WebUI.Api.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
</MessageDetail>
</Error>
采纳答案by Kartikeya Khosla
I think looking at your update your action requires idwhich is of non-nullable intand you are not supplying it just modify url as http://localhost:21879/API/products/GetAllProducts/2here '2'is assigned to 'id'and it will work.
我认为查看您的更新您的操作需要id哪些是不可为空的,int并且您没有提供它只是修改 url,因为http://localhost:21879/API/products/GetAllProducts/2这里'2'被分配给'id'它,它会起作用。
In WebApiConfig modify route as :
在 WebApiConfig 中修改路由为:
config.Routes. MapHttpRoute (
name: "DefaultApi" ,
routeTemplate: "api/{controller}/{action}/{id}" ,
defaults: new { id = RouteParameter . Optional }
);
Just change this Mvc Route :
只需更改此 Mvc 路由:
routes. MapRoute(
name: "DefaultApi" ,
url: "API/{controller}/{action}/{id}" ,
defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
);
with this :
有了这个 :
routes. MapRoute(
name: "DefaultApiMvc" ,
url: "APIMvc/{controller}/{action}/{id}" ,
defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
);
because otherwise Mvc and Api routes will conflict with each other...
因为否则 Mvc 和 Api 路由会相互冲突......
回答by Kevin
I had the same problem. Watch the order in which you register your routes in Global.asax.cs.
我有同样的问题。观察您在 Global.asax.cs 中注册路由的顺序。
This works:
这有效:
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
This doesn't:
这不会:
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
回答by Tallmaris
If you use that way of routing, you need to specify the allowed Http methods for the action:
如果使用这种路由方式,则需要为操作指定允许的 Http 方法:
public class ProductsController : ApiController
{
[HttpGet]
public string GetAllProducts()
{
return "Hello From WebAPI";
}
}
You can get all the information regarding API routing from this link: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
您可以从此链接获取有关 API 路由的所有信息:http: //www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

