From b4d10b7bff0e354e8db32428e9a6373db5e8fdc1 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Mon, 12 Jun 2017 12:46:00 +0300 Subject: [PATCH] Refactor WebApi --- DAL/DAL.csproj | 3 + Foorum.sln | 20 +++--- Foorum/Web.config | 31 --------- Foorum/packages.config | 6 -- WebApi/App_Start/NinjectWebCommon.cs | 66 ++++++++++++++++++ WebApi/App_Start/WebApiConfig.cs | 51 ++++++++++++++ WebApi/Controllers/FoorumController.cs | 39 +++++++++++ WebApi/Global.asax | 1 + WebApi/Global.asax.cs | 17 +++++ {Foorum => WebApi}/Properties/AssemblyInfo.cs | 6 +- {Foorum => WebApi}/Web.Debug.config | 0 {Foorum => WebApi}/Web.Release.config | 0 WebApi/Web.config | 46 +++++++++++++ Foorum/Foorum.csproj => WebApi/WebApi.csproj | 67 ++++++++++++++----- WebApi/index.html | 2 + WebApi/packages.config | 16 +++++ 16 files changed, 305 insertions(+), 66 deletions(-) delete mode 100644 Foorum/Web.config delete mode 100644 Foorum/packages.config create mode 100644 WebApi/App_Start/NinjectWebCommon.cs create mode 100644 WebApi/App_Start/WebApiConfig.cs create mode 100644 WebApi/Controllers/FoorumController.cs create mode 100644 WebApi/Global.asax create mode 100644 WebApi/Global.asax.cs rename {Foorum => WebApi}/Properties/AssemblyInfo.cs (90%) rename {Foorum => WebApi}/Web.Debug.config (100%) rename {Foorum => WebApi}/Web.Release.config (100%) create mode 100644 WebApi/Web.config rename Foorum/Foorum.csproj => WebApi/WebApi.csproj (69%) create mode 100644 WebApi/index.html create mode 100644 WebApi/packages.config diff --git a/DAL/DAL.csproj b/DAL/DAL.csproj index 3a3287a..125f5c9 100644 --- a/DAL/DAL.csproj +++ b/DAL/DAL.csproj @@ -47,7 +47,10 @@ + + + diff --git a/Foorum.sln b/Foorum.sln index 3ccea43..f8114cd 100644 --- a/Foorum.sln +++ b/Foorum.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26403.7 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Foorum", "Foorum\Foorum.csproj", "{D4668470-A7AB-4F03-912A-BE9187967E36}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DAL", "DAL\DAL.csproj", "{B783887A-E5BB-4066-9B9B-ABD7B4C1E269}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csproj", "{69C81CEE-28DC-4B57-B9B5-F8CDE8E22750}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi", "WebApi\WebApi.csproj", "{E8D27F06-E3F7-4304-899F-ADFC69297E7E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,14 +13,14 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D4668470-A7AB-4F03-912A-BE9187967E36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D4668470-A7AB-4F03-912A-BE9187967E36}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D4668470-A7AB-4F03-912A-BE9187967E36}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D4668470-A7AB-4F03-912A-BE9187967E36}.Release|Any CPU.Build.0 = Release|Any CPU - {69C81CEE-28DC-4B57-B9B5-F8CDE8E22750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {69C81CEE-28DC-4B57-B9B5-F8CDE8E22750}.Debug|Any CPU.Build.0 = Debug|Any CPU - {69C81CEE-28DC-4B57-B9B5-F8CDE8E22750}.Release|Any CPU.ActiveCfg = Release|Any CPU - {69C81CEE-28DC-4B57-B9B5-F8CDE8E22750}.Release|Any CPU.Build.0 = Release|Any CPU + {B783887A-E5BB-4066-9B9B-ABD7B4C1E269}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B783887A-E5BB-4066-9B9B-ABD7B4C1E269}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B783887A-E5BB-4066-9B9B-ABD7B4C1E269}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B783887A-E5BB-4066-9B9B-ABD7B4C1E269}.Release|Any CPU.Build.0 = Release|Any CPU + {E8D27F06-E3F7-4304-899F-ADFC69297E7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8D27F06-E3F7-4304-899F-ADFC69297E7E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8D27F06-E3F7-4304-899F-ADFC69297E7E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8D27F06-E3F7-4304-899F-ADFC69297E7E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Foorum/Web.config b/Foorum/Web.config deleted file mode 100644 index 5e6429c..0000000 --- a/Foorum/Web.config +++ /dev/null @@ -1,31 +0,0 @@ - - - - - -
- - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Foorum/packages.config b/Foorum/packages.config deleted file mode 100644 index d15da3c..0000000 --- a/Foorum/packages.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/WebApi/App_Start/NinjectWebCommon.cs b/WebApi/App_Start/NinjectWebCommon.cs new file mode 100644 index 0000000..f8fc7da --- /dev/null +++ b/WebApi/App_Start/NinjectWebCommon.cs @@ -0,0 +1,66 @@ +[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebApi.App_Start.NinjectWebCommon), "Start")] +[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(WebApi.App_Start.NinjectWebCommon), "Stop")] + +namespace WebApi.App_Start +{ + using System; + using System.Web; + + using Microsoft.Web.Infrastructure.DynamicModuleHelper; + + using Ninject; + using Ninject.Web.Common; + + public static class NinjectWebCommon + { + private static readonly Bootstrapper bootstrapper = new Bootstrapper(); + + /// + /// Starts the application + /// + public static void Start() + { + DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); + DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); + bootstrapper.Initialize(CreateKernel); + } + + /// + /// Stops the application. + /// + public static void Stop() + { + bootstrapper.ShutDown(); + } + + /// + /// Creates the kernel that will manage your application. + /// + /// The created kernel. + private static IKernel CreateKernel() + { + var kernel = new StandardKernel(); + try + { + kernel.Bind>().ToMethod(ctx => () => new Bootstrapper().Kernel); + kernel.Bind().To(); + + RegisterServices(kernel); + return kernel; + } + catch + { + kernel.Dispose(); + throw; + } + } + + /// + /// Load your modules or register your services here! + /// + /// The kernel. + private static void RegisterServices(IKernel kernel) + { + } + } +} diff --git a/WebApi/App_Start/WebApiConfig.cs b/WebApi/App_Start/WebApiConfig.cs new file mode 100644 index 0000000..933c8de --- /dev/null +++ b/WebApi/App_Start/WebApiConfig.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Formatting; +using System.Net.Http.Headers; +using System.Web.Http; + +namespace WebApi +{ + public class JsonContentNegotiator : IContentNegotiator + { + private readonly JsonMediaTypeFormatter _jsonFormatter; + + public JsonContentNegotiator(JsonMediaTypeFormatter formatter) + { + _jsonFormatter = formatter; + } + + public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable formatters) + { + var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json")); + return result; + } + } + + public static class WebApiConfig + { + public static void Register(HttpConfiguration config) + { + // Web API configuration and services + + //DEBUG + config.EnableSystemDiagnosticsTracing(); + + // Web API routes + config.MapHttpAttributeRoutes(); + + config.Routes.MapHttpRoute( + name: "DefaultApi", + routeTemplate: "api/{controller}/{id}", + defaults: new { id = RouteParameter.Optional } + ); + + // Force always json response + var jsonFormatter = new JsonMediaTypeFormatter(); + //optional: set serializer settings here + config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); + } + } +} diff --git a/WebApi/Controllers/FoorumController.cs b/WebApi/Controllers/FoorumController.cs new file mode 100644 index 0000000..a0947dc --- /dev/null +++ b/WebApi/Controllers/FoorumController.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace WebApi.Controllers +{ + public class FoorumController : ApiController + { + // GET api/ + public IEnumerable Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api//5 + public string Get(int id) + { + return "value"; + } + + // POST api/ + public void Post([FromBody]string value) + { + } + + // PUT api//5 + public void Put(int id, [FromBody]string value) + { + } + + // DELETE api//5 + public void Delete(int id) + { + } + } +} \ No newline at end of file diff --git a/WebApi/Global.asax b/WebApi/Global.asax new file mode 100644 index 0000000..5d67dae --- /dev/null +++ b/WebApi/Global.asax @@ -0,0 +1 @@ +<%@ Application Codebehind="Global.asax.cs" Inherits="WebApi.WebApiApplication" Language="C#" %> diff --git a/WebApi/Global.asax.cs b/WebApi/Global.asax.cs new file mode 100644 index 0000000..23b4419 --- /dev/null +++ b/WebApi/Global.asax.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Http; +using System.Web.Routing; + +namespace WebApi +{ + public class WebApiApplication : System.Web.HttpApplication + { + protected void Application_Start() + { + GlobalConfiguration.Configure(WebApiConfig.Register); + } + } +} diff --git a/Foorum/Properties/AssemblyInfo.cs b/WebApi/Properties/AssemblyInfo.cs similarity index 90% rename from Foorum/Properties/AssemblyInfo.cs rename to WebApi/Properties/AssemblyInfo.cs index 280d8dd..1265d99 100644 --- a/Foorum/Properties/AssemblyInfo.cs +++ b/WebApi/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("Foorum")] +[assembly: AssemblyTitle("WebApi")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Foorum")] +[assembly: AssemblyProduct("WebApi")] [assembly: AssemblyCopyright("Copyright © 2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -20,7 +20,7 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d4668470-a7ab-4f03-912a-be9187967e36")] +[assembly: Guid("e8d27f06-e3f7-4304-899f-adfc69297e7e")] // Version information for an assembly consists of the following four values: // diff --git a/Foorum/Web.Debug.config b/WebApi/Web.Debug.config similarity index 100% rename from Foorum/Web.Debug.config rename to WebApi/Web.Debug.config diff --git a/Foorum/Web.Release.config b/WebApi/Web.Release.config similarity index 100% rename from Foorum/Web.Release.config rename to WebApi/Web.Release.config diff --git a/WebApi/Web.config b/WebApi/Web.config new file mode 100644 index 0000000..bf7cddb --- /dev/null +++ b/WebApi/Web.config @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Foorum/Foorum.csproj b/WebApi/WebApi.csproj similarity index 69% rename from Foorum/Foorum.csproj rename to WebApi/WebApi.csproj index 600e2f6..b9951a6 100644 --- a/Foorum/Foorum.csproj +++ b/WebApi/WebApi.csproj @@ -8,13 +8,13 @@ 2.0 - {D4668470-A7AB-4F03-912A-BE9187967E36} + {E8D27F06-E3F7-4304-899F-ADFC69297E7E} {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} Library Properties - Foorum - Foorum - v4.6.1 + WebApi + WebApi + v4.5.2 true @@ -43,16 +43,23 @@ 4 - - ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll - - - ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll - ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.5\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + ..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\Ninject.3.2.2.0\lib\net45-full\Ninject.dll + + + ..\packages\Ninject.Web.Common.3.2.0.0\lib\net45-full\Ninject.Web.Common.dll + + @@ -62,6 +69,9 @@ + + ..\packages\Microsoft.AspNet.WebApi.Tracing.5.2.3\lib\net45\System.Web.Http.Tracing.dll + @@ -69,6 +79,33 @@ + + ..\packages\WebActivatorEx.2.0\lib\net40\WebActivatorEx.dll + + + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll + + + + + + + + + + + + Global.asax + + @@ -80,10 +117,8 @@ - - - - + + 10.0 @@ -98,9 +133,9 @@ True True - 1721 + 2430 / - http://localhost:1721/ + http://localhost:2430/ False False diff --git a/WebApi/index.html b/WebApi/index.html new file mode 100644 index 0000000..d339520 --- /dev/null +++ b/WebApi/index.html @@ -0,0 +1,2 @@ +

api

+foorum \ No newline at end of file diff --git a/WebApi/packages.config b/WebApi/packages.config new file mode 100644 index 0000000..d945096 --- /dev/null +++ b/WebApi/packages.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file