甜瓜
水穿石,冰千尺,岂曰一日功
1.新建项目 ---> 选择 web 应用程序 选择 webApi
2. 创建一个httpmodeule类 放到app_data文件夹下
 public class MyHttpModule : IHttpModule
    {
        #region IHttpModule 成员
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            string url = context.Request.FilePath;
            string json = string.Empty;
            try
            {
                if (url.Contains("/api/"))//只要访问路径是接口的都要验证token
                {
                }
            }
            catch
            {
                json = "{\"resultCode\": \"0\",\"data\":{\"returnInfo\":\"服务器异常\"}}";
                context.Response.Write(json);
                context.Response.End();
            }
        }
        #endregion
    }
2.在app strat 文件夹下面 WebApiConfig 修改成如下代码
 public static void Register(HttpConfiguration config)
        {
            // Web API 路由
            config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{action}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            var appjsonType = config.Formatters.JsonFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "text/json");
            config.Formatters.JsonFormatter.SupportedMediaTypes.Remove(appjsonType);
        }
3.项目管理nuget包
AttributeRouting 搜索 安装如下nuget包
AttributeRouting (ASP.NET Web API) 选择安装
 
4.接下来在webconfig中配置文件
在<system.web> 节点下面 配置
   <httpModules>
      <add name="项目名称.MyHttpModule" type="项目名称.MyHttpModule" />
    </httpModules>

  

在 <system.webServer>节点西面配置 添加如下内容
 
<validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="项目名称.MyHttpModule" type="项目名称.MyHttpModule" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="AttributeRouting" path="routes.axd" verb="*" type="AttributeRouting.Web.Logging.LogRoutesHandler, AttributeRouting.Web" />
    </handlers>
    <directoryBrowse enabled="true" />

  好了  写到这里 程序 就OK 了  高手轻喷  

 
 
 
 
 

  

posted on 2016-10-12 14:59  甜瓜1  阅读(1001)  评论(2编辑  收藏  举报