using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Data.Entity; using Weee.Models; using Weee.DAL; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Weee.Service; using Weee.Models.Paramemter; using CScommon; using System.Security.Claims; using Resources; using NLog; using System.Web.Http.Controllers; using System.Web; namespace Weee.Controllers { [Authorize(Roles = ProgramConstants.normalcompany)] //[Filter.ApiMultilanguage]disable obsolete warning, not sure OK or not public class ApiControllerBase : ApiController { protected WeeeDataAuthorizeService _authorizeService; protected Dictionary activityItems = new Dictionary(); protected Dictionary emitParaItems = new Dictionary(); protected Logger log; //protected string baseUrl = ""; public ApiControllerBase(WeeeDataAuthorizeService s) { _authorizeService = s; _authorizeService.Initialize(User.Identity.GetUserId()); //db = d; //List activityItems = new List(); //activityItems.Add(0, Resource.StaticLabelGlobal_Select); activityItems.Add( 1, Resource.autoContinuousMeasure ); activityItems.Add( 2, Resource.indirectMeasure ); activityItems.Add( 3, Resource.selfEstimate ); //tempd.activityItems = activityItems; //List emitParaItems = new List(); //emitParaItems.Add(0, Resource.StaticLabelGlobal_Select); emitParaItems.Add( 1, Resource.measureEnergyBalance ); emitParaItems.Add( 2, Resource.sameSituationExperience ); emitParaItems.Add( 3, Resource.factorFromManufacturer ); emitParaItems.Add( 4, Resource.regionEmissionFactor ); emitParaItems.Add( 5, Resource.nationEmissionFactor ); emitParaItems.Add( 6, Resource.internationalFactor ); //ViewBag.emitParaItems = emitParaItems; log = NLog.LogManager.GetCurrentClassLogger(); } /** * Shared code for LCA SaveHeader API */ protected HttpResponseMessage SaveSheetHeader(WeeeSheetDataService service, Categories category, SheetHeader header) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } header.Category = category; service.SaveSheetHeader(header); return Request.CreateResponse(HttpStatusCode.OK, 0); } protected HttpResponseMessage SaveSheetHeader(WaterUsageDataService service, Categories category, SheetHeader header) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } header.Category = category; service.SaveSheetHeader(header); return Request.CreateResponse(HttpStatusCode.OK, 0); } protected string GetCurrentUserId() { var principal = User as ClaimsPrincipal; var claim = principal.FindFirst(ClaimTypes.NameIdentifier); return claim != null ? claim.Value : null; } protected string getBaseUrl() { string baseUrl = Request.RequestUri.ToString();//.GetLeftPart(UriPartial.Authority); //Logger log = NLog.LogManager.GetCurrentClassLogger(); //log.Info($"getBaseUrl {Request.RequestUri} {baseUrl}"); string actPath = HttpContext.Current.Request.Url.Authority; string protocolName; if (HttpContext.Current.Request.IsSecureConnection) protocolName = "https://"; else protocolName = "http://"; string removeLeft = protocolName + actPath; if (baseUrl.IndexOf(removeLeft)==0) baseUrl = baseUrl.Substring(removeLeft.Length, baseUrl.Length-removeLeft.Length); else { return ""; } //log.Info($"getBaseUrl 2 {baseUrl} {protocolName} {actPath}"); if (baseUrl.Length > 0) { string[] words = baseUrl.Split('/'); if (words.Length > 0) { if (words[1] == "api") baseUrl = ""; else baseUrl = "/" + words[1]; } else baseUrl = ""; //if (baseUrl.Substring(baseUrl.Length - 1) == "/") // baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } //log.Info($"getBaseUrl 3 {baseUrl}"); return baseUrl; } } }