代码:
WebApiConfig
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web.Http; using System.Configuration; using Microsoft.Owin.Security.OAuth; using Newtonsoft.Json.Serialization; using System.Web.Http.Cors; using System.Web.Http.WebHost; using System.Web.SessionState; using System.Web.Routing; using System.Web; namespace WorkOrderManage.API { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API 配置和服务 // 将 Web API 配置为仅使用不记名令牌身份验证。 config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); //// Web API 路由 //config.MapHttpAttributeRoutes(); //config.Routes.MapHttpRoute( // name: "DefaultApi", // routeTemplate: "api/{controller}/{action}/{id}", // defaults: new { id = RouteParameter.Optional, namespaceName = "WorkOrderManage.API.Controllers" } //); // 全局里面已经有了,如果添加这个路由,那么全局里面的就会无效 // config.Routes.MapHttpRoute( // name: "Admin Area Default", // routeTemplate: "api/admin/{controller}/{action}/{id}", // defaults: new { id = RouteParameter.Optional, namespaceName = "WorkOrderManage.API.Areas.Admin.Controllers" } //); } } public class SessionRouteHandler : HttpControllerHandler, IRequiresSessionState { public SessionRouteHandler(RouteData routeData) : base(routeData) { } } public class SessionControllerRouteHandler : HttpControllerRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { return new SessionRouteHandler(requestContext.RouteData); } } }
添加下面的两个类,注释掉里面的路由,在全局里面添加路由
Global.asax
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Formatting; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using System.Web.SessionState; using WorkOrderManage.API.Utility; namespace WorkOrderManage.API { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var jsonFormatter = new JsonMediaTypeFormatter(); GlobalConfiguration.Configuration.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); RouteTable.Routes.MapHttpRoute( name: "Admin Area Default", routeTemplate: "api/admin/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional, namespaceName = "WorkOrderManage.API.Areas.Admin.Controllers" } ).RouteHandler = new SessionControllerRouteHandler(); RouteTable.Routes.MapHttpRoute( name: "Email Area Default", routeTemplate: "api/email/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional, namespaceName = "WorkOrderManage.API.Areas.Email.Controllers" } ).RouteHandler = new SessionControllerRouteHandler(); } public override void Init() { PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest; base.Init(); } void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) { HttpContext.Current.SetSessionStateBehavior( SessionStateBehavior.Required); } } }
在这个里面添加路由
不能在全局里添加路由,又在webapiconfig里面添加一个同名的路由
http://www.cnblogs.com/firstcsharp/p/4482835.html
时间: 2024-12-17 17:06:20