的WebAPI路由 - 地图网址API [英] WebApi Routing - map url to api
问题描述
我一直在谷歌上搜索了大约一个小时,没有发现一个例子,所以我想我会在这里问。
I've been googling for about an hour and not found an example so I thought I'd ask here.
我用的WebAPI更换标准的MVC控制器和具有路由的问题,我已经改变,因为该组织的名字,但他们仍然是有效的。
I am replacing a standard mvc controller with a webapi and having problems with the routing, I've changed the names because of the organisation but they are still valid.
我的WebAPI目前称为SystemAPI在控制器中的文件夹 - 我希望它有一个不同的名称,理想指向的URL
my webapi is currently called SystemAPI in the controllers folder - I want it to have a different name that the url that points to it ideally.
我需要指向的URL是/ v1Controller / {ID}
我不能改变v1Controller URL,因为它是应用程序的一个固定点我有超过
The url I need to point to it is /v1Controller/{id} I cant change the v1Controller url as it is a fixed point with apps I have no control over
我目前的编码尝试
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DocumobiApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "V1Controller",
routeTemplate: "v1Controller/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
}
而我得到的回应
"message":"No HTTP resource was found that matches the request URI 'http://localhost:38685/v1Controller'.","messageDetail":"No route providing a controller name was found to match request URI 'http://localhost:38685/v1Controller'"}
我敢肯定它的相当明显,但不能为我的生活中的东西看着办吧。
I'm sure its something fairly obvious but cant for the life of me figure it out.
干杯,
史蒂芬
推荐答案
由于以下错误消息称,一个路由匹配应导致的控制器
路径变量提供价值哪些Web API取决于选择的控制器。
As the following error message says, a route match should result in providing value for the controller
route variable which Web API depends on for selecting a controller.
No route providing a controller name was found to match request URI 'http://localhost:38685/v1Controller
所以,你可以修改类似下面的路径(注意为控制器
变量的默认值):
config.Routes.MapHttpRoute(
name: "V1Controller",
routeTemplate: "v1Controller/{id}",
defaults: new { id = RouteParameter.Optional, controller="SomeControllerHere" }
);
这篇关于的WebAPI路由 - 地图网址API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!