111*_*110 8 asp.net-mvc asp.net-web-api asp.net-web-api-routing asp.net-web-api2
我第一次使用Web API将我的移动应用程序连接到Web API.
我有MVC 5项目,我已经创建了API文件夹ProductsController.
namespace DDD.WebUI.API
{
public class ProductsController : ApiController
{
public string GetAllProducts()
{
return "Hello From WebAPI";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图从浏览器访问此方法:
http://localhost:21879/DDD.WebUI/API/products/GetAllProducts
Run Code Online (Sandbox Code Playgroud)
但我得到了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 }
);
}
Run Code Online (Sandbox Code Playgroud)
更新2
我有一点进展:我有更新Application_Start():
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)
在WebApiConfig我有:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我仍然在Project/Api/ProductsController中存储API控制器但是现在我可以访问它但仍然无法从中获取值:
http://localhost:21879/api/products/getallproducts
Run Code Online (Sandbox Code Playgroud)
但我得到的错误是:
<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>
Run Code Online (Sandbox Code Playgroud)
小智 7
我有同样的问题.观察您在Global.asax.cs中注册路由的顺序.
这有效:
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)
这不是:
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
Run Code Online (Sandbox Code Playgroud)
我认为查看您的更新,您的操作需要id不可为空的内容int,并且您没有提供它,只需修改http://localhost:21879/API/products/GetAllProducts/2 此处'2'分配的网址'id'即可工作。
在 WebApiConfig 中将路由修改为:
config.Routes. MapHttpRoute (
name: "DefaultApi" ,
routeTemplate: "api/{controller}/{action}/{id}" ,
defaults: new { id = RouteParameter . Optional }
);
Run Code Online (Sandbox Code Playgroud)
只需更改此 Mvc 路由:
routes. MapRoute(
name: "DefaultApi" ,
url: "API/{controller}/{action}/{id}" ,
defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
);
Run Code Online (Sandbox Code Playgroud)
有了这个 :
routes. MapRoute(
name: "DefaultApiMvc" ,
url: "APIMvc/{controller}/{action}/{id}" ,
defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
);
Run Code Online (Sandbox Code Playgroud)
因为否则 Mvc 和 Api 路由会相互冲突......
| 归档时间: |
|
| 查看次数: |
24504 次 |
| 最近记录: |