Asp.net web api 如何从WebApi中删除默认路由?
我找不到删除基于WebApi的移动服务项目的默认路由路径的方法。它生成诸如“/api/{controller}/{id}”和“/tables/{controller}/{id}”之类的路由,我不想为客户端应用程序提供这些路由。相反,我正在映射这样的路由-“/api_v1/{controller}/{id}”,并希望将其作为访问数据的一种可能的路由 以下是不起作用的代码:Asp.net web api 如何从WebApi中删除默认路由?,asp.net-web-api,Asp.net Web Api,我找不到删除基于WebApi的移动服务项目的默认路由路径的方法。它生成诸如“/api/{controller}/{id}”和“/tables/{controller}/{id}”之类的路由,我不想为客户端应用程序提供这些路由。相反,我正在映射这样的路由-“/api_v1/{controller}/{id}”,并希望将其作为访问数据的一种可能的路由 以下是不起作用的代码: public static class WebApiConfig { public static void Regis
public static class WebApiConfig
{
public static void Register()
{
// Use this class to set configuration options for your mobile service
ConfigOptions options = new ConfigOptions();
options.LoginProviders.Add(typeof(CustomLoginProvider));
// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
// add login provider
config.SetIsHosted(true);
//foreach (var route in config.Routes)
// Console.WriteLine(route.ToString());
config.Routes.MapHttpRoute(
name: "api_v1",
routeTemplate: "api_v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, } );
//Create the object of particular router
string result = "";
foreach (var r in config.Routes)
result += r.ToString();
// This does not remove any route.
// Also there is no 'Name' property for Route to know exact match
config.Routes.Remove("api");
}
}
更新:
我为我拥有的每个可能的路由调用config.Routes.Remove(“”),它只删除一个名称“tables”。但“api”路线仍然存在
ServiceConfig.Config.Routes.Remove("DefaultApi"); // not removed any
ServiceConfig.Config.Routes.Remove("api"); // not removed any route
ServiceConfig.Config.Routes.Remove("tables"); // this works!
如何删除默认的“api”路由?在
App\u Start
中,您应该有一个名为RouteConfig
的文件
在该类中有一个名为RegisterRoutes
的方法,您将在其中看到预配置的路由映射
您可以根据需要移除这些
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
此外,如果要使用“按MVC路由”属性,可以添加以下行:
routes.MapMvcAttributeRoutes();
好的,应该为此使用config.Routes.Remove方法。要获取要删除的路由名称,可以在调试器中查看它们。 默认路由名称的名称为“defaultAPI” 因此,该代码起作用:
ServiceConfig.Config.Routes.Remove("DefaultApis");
ServiceConfig.Config.Routes.Remove("tables");
WebAPI中没有生成“默认”路由的内容(尤其是带有“tables”前缀的)。您的代码(或您使用的任何库)中应该有设置它们的内容。该项目是从“Azure移动服务”模板创建的,在某个地方它生成了15条默认路由,我在debbuger中看到了。您是否尝试过在
ServiceConfig
中查找?请在哪里?在Web.config或Global.asax或项目中的任何文件中都没有相关内容。您在此行中调用它:HttpConfiguration config=ServiceConfig.Initialize(新的ConfigBuilder(选项))代码>我知道RouteConfig,但没有Azure Mobile服务的此类文件。因此,没有什么可以删除的。