Web Api Route 注册要放在 Mvc Route 注册前
今天想研究一下Web Api,写了一个测试Api,打开网站后浏览一下,可是却提示找不到方法,刚开始以为哪里配置错了,可找了半天也没见。
因为我是在一个现有Mvc站点做的Demo,所以打算新建一个Mvc网站,再试一下,新站点是正常的,对比一下Global文件,发现WebApiConfig和RouteConfig顺序不一样。
如果把新站点的RouteConfig也放在WebApiConfig之前,同样提示找不到方法。看来这两个配置有关联呀。
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
看了一下GlobalConfiguration类就明白了
public static class GlobalConfiguration
{
private static Lazy<HttpConfiguration> _configuration = new Lazy<HttpConfiguration>(delegate {
HttpConfiguration configuration = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes));
configuration.Services.Replace(typeof(IAssembliesResolver), new WebHostAssembliesResolver());
configuration.Services.Replace(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver());
configuration.Services.Replace(typeof(IHostBufferPolicySelector), new WebHostBufferPolicySelector());
return configuration;
});
public static HttpConfiguration Configuration
{
get
{
return _configuration.Value;
}
} //...
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "t/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
} public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
合并后的类似路由:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
如果顺序反了,DefaultApi的路由就永远匹配不到了,会一直找MVC的路由"{api}/{action}/{id}"。
Web Api Route 注册要放在 Mvc Route 注册前的更多相关文章
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- 从ASP.Net Core Web Api模板中移除MVC Razor依赖项
前言 :本篇文章,我将会介绍如何在不包括MVC / Razor功能和包的情况下,添加最少的依赖项到ASP.NET Core Web API项目中. 一.MVC VS WebApi (1)在ASP. ...
- .net web api 的route理解
.NET web api 的特性是和MVC一样,通过Route 来控制action的访问方式.Route匹配规则是个奇特的方式,首先看一段Route的模板 Routes.MapHttpRoute( n ...
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
- 【ASP.NET MVC 5】第27章 Web API与单页应用程序
注:<精通ASP.NET MVC 3框架>受到了出版社和广大读者的充分肯定,这让本人深感欣慰.目前该书的第4版不日即将出版,现在又已开始第5版的翻译,这里先贴出该书的最后一章译稿,仅供大家 ...
- 使用 ASP.NET Core MVC 创建 Web API(二)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
- 使用 ASP.NET Core MVC 创建 Web API(三)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 十 ...
- [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到现有的 Asp.Net MVC 项目中 解决方案 在 Visual Studio 2012 中就已经把 Asp.Net Web Api 自动地整合 ...
- MVC和Web API 过滤器Filter
MVC和Web API Filter(过滤器) ASP.NET MVC 支持以下类型的操作筛选器: · 授权筛选器.这些筛选器用于实现IAuthorizationFilter和做出关于是 ...
随机推荐
- 《Java4Android视频教程》学习笔记(三)
一:抽象类 接口 1.使用abstract修饰抽象类 抽象函数 2.一个类中有一个或者多个抽象函数,必须定义为抽象类 3.抽象类可以不含有抽象函数 4.抽象类不可以生成对象 tip:如果一个代码在语意 ...
- ExpandableListView(三)只展开一个group,没有child不展开group
本文是自己在实践中,发现的问题. 有时候想让界面更加的人性化,就要实现很多的效果,比如只展开一个group,在点击下个group的同时,关闭之前的group 在一个ExpandableListView ...
- SQL基础查询实战
总结: 一.单表查询的情况: 1.where.....group by .. Having count(..) 2.Group by haing min(..)条件and max(..)条件(查询最低 ...
- Session会话跟踪
用encodeURL重写URL public class SessionA extends HttpServlet { @Override protected void doGet(HttpServl ...
- C语言:类似linux内核的分等级DEBUG宏(打印宏)
总结几种log打印printf函数的宏定义 http://blog.chinaunix.net/uid-20564848-id-73402.html #include <stdio.h> ...
- #ifndef 与 #program once 的区别(转)
转自http://hi.baidu.com/hrx20091001/item/ee70f7cc6d036d4ea9ba94e0 #ifndef 与 #program once 的区别 为了避免同一个文 ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...
- 开源流媒体处理库live555服务器端、客户端源代码分析总结
RTSP服务器端流程: 1. RTSP连接的建立过程 RTSPServer类用于构建一个RTSP服务器,该类同时在其内部定义了一个RTSPClientSession类,用于处理单独的客户会话. 首先创 ...
- php定界符 <<< 的作用及使用注意事项
按照原样输出,包括换行符.特殊字符等 任何特殊字符都不需要转义,比如双引号.单引号,它会按照原样输出 像在双引号字符串中一样使用php的变量输出 定界符<<<,当需要输出大段文本时, ...
- Oracle select into from 和 insert into select
select into from SQLSERVER 创建表: select * into aaa from bbb Oracle 创建表: create table aaa as select * ...