Skip to content

Commit

Permalink
#3 Moving Core.Web
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Gerbenis committed Sep 17, 2015
1 parent 632f409 commit 41b8d50
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 83 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Web.Routing;
using Autofac;
using BetterModules.Core.Environment.Assemblies;
using BetterModules.Core.Environment.Assemblies;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Web.Mvc.Extensions;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace BetterModules.Core.Web.Modules.Registration
{
Expand All @@ -21,8 +22,8 @@ public class DefaultWebModulesRegistration : DefaultModulesRegistration, IWebMod
/// </summary>
/// <param name="assemblyLoader">The assembly loader.</param>
/// <param name="controllerExtensions">The controller extensions.</param>
public DefaultWebModulesRegistration(IAssemblyLoader assemblyLoader, IControllerExtensions controllerExtensions)
: base(assemblyLoader)
public DefaultWebModulesRegistration(IAssemblyLoader assemblyLoader, IControllerExtensions controllerExtensions, ILoggerFactory loggerFactory)
: base(assemblyLoader, loggerFactory)
{
this.controllerExtensions = controllerExtensions;
}
Expand Down Expand Up @@ -66,10 +67,7 @@ public void RegisterKnownModuleRoutes(RouteCollection routes)
var webModuleContext = context.Value as WebModuleRegistrationContext;
if (webModuleContext != null)
{
foreach (var moduleRoute in webModuleContext.Routes)
{
routes.Add(moduleRoute);
}
routes.Add(webModuleContext.Routes);
}
}
}
Expand All @@ -78,8 +76,8 @@ public void RegisterKnownModuleRoutes(RouteCollection routes)
/// Registers the types.
/// </summary>
/// <param name="registrationContext">The registration context.</param>
/// <param name="containerBuilder">The container builder.</param>
protected override void RegisterModuleDescriptor(ModuleRegistrationContext registrationContext, ContainerBuilder containerBuilder)
/// <param name="services"></param>
protected override void RegisterModuleDescriptor(ModuleRegistrationContext registrationContext, IServiceCollection services)
{
var webContext = registrationContext as WebModuleRegistrationContext;
if (webContext != null)
Expand All @@ -90,7 +88,7 @@ protected override void RegisterModuleDescriptor(ModuleRegistrationContext regis
webDescriptor.RegisterCustomRoutes(webContext, containerBuilder);
}

base.RegisterModuleDescriptor(registrationContext, containerBuilder);
base.RegisterModuleDescriptor(registrationContext, services);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Web.Routing;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Modules.Registration;
using Microsoft.AspNet.Routing;

namespace BetterModules.Core.Web.Modules.Registration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using BetterModules.Core.Modules.Registration;
using Microsoft.AspNet.Routing;

namespace BetterModules.Core.Web.Modules.Registration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using Autofac;
using BetterModules.Core.Web.Dependencies;

using System;
using Microsoft.Framework.DependencyInjection;

namespace BetterModules.Core.Web.Mvc.Commands
{
public class DefaultCommandResolver : ICommandResolver
{
private readonly PerWebRequestContainerProvider containerProvider;
private readonly IServiceProvider serviceProvider;

public DefaultCommandResolver(PerWebRequestContainerProvider containerProvider)
public DefaultCommandResolver(IServiceProvider serviceProvider)
{
this.containerProvider = containerProvider;
this.serviceProvider = serviceProvider;
}

public TCommand ResolveCommand<TCommand>(ICommandContext context) where TCommand : ICommandBase
{
if (containerProvider.CurrentScope.IsRegistered<TCommand>())
var command = serviceProvider.GetService<TCommand>();
if (command != null)
{
var command = containerProvider.CurrentScope.Resolve<TCommand>();
command.Context = context;

command.Context = context;
return command;
}

Expand Down
23 changes: 12 additions & 11 deletions vNext/src/BetterModules.Core.Web/Mvc/CoreControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;
using BetterModules.Core.Web.Models;
using BetterModules.Core.Web.Mvc.Commands;
using BetterModules.Core.Web.Mvc.Extensions;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;

namespace BetterModules.Core.Web.Mvc
{
Expand Down Expand Up @@ -85,12 +86,12 @@ public virtual IMessagesIndicator Messages
/// Creates a <see cref="T:System.Web.Mvc.JsonResult" /> object that serializes the specified object to JavaScript Object Notation (JSON) format.
/// </summary>
/// <param name="data">The JavaScript object graph to serialize.</param>
/// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
/// <returns>
/// The JSON result object that serializes the specified object to JSON format.
/// </returns>
[NonAction]
public virtual JsonResult Json(WireJson data, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
public virtual JsonResult Json(WireJson data/*, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet*/)
{
List<string> messages = data.Messages != null
? data.Messages.ToList()
Expand All @@ -99,22 +100,22 @@ public virtual JsonResult Json(WireJson data, JsonRequestBehavior behavior = Jso
messages.AddRange(data.Success ? Messages.Success : Messages.Error);
data.Messages = messages.ToArray();

return base.Json(data, behavior);
return base.Json(data/*, behavior*/);
}

/// <summary>
/// Creates a <see cref="T:System.Web.Mvc.JsonResult" /> object that serializes the specified object to JavaScript Object Notation (JSON) format.
/// </summary>
/// <param name="success">The request result.</param>
/// <param name="data">The JavaScript object graph to serialize.</param>
/// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
/// <returns>
/// The JSON result object that serializes the specified object to JSON format.
/// </returns>
[NonAction]
public virtual JsonResult WireJson(bool success, object data = null, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
public virtual JsonResult WireJson(bool success, object data = null/*, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet*/)
{
return Json(new WireJson { Success = success, Data = data }, behavior);
return Json(new WireJson { Success = success, Data = data }/*, behavior*/);
}

/// <summary>
Expand All @@ -123,14 +124,14 @@ public virtual JsonResult WireJson(bool success, object data = null, JsonRequest
/// <param name="success">The request result.</param>
/// <param name="html">The HTML.</param>
/// <param name="data">The JavaScript object graph to serialize.</param>
/// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
// <param name="behavior">Specifies whether HTTP GET requests from the client are allowed.</param>
/// <returns>
/// The JSON result object that serializes the specified object to JSON format combined with Html.
/// </returns>
[NonAction]
public virtual JsonResult ComboWireJson(bool success, string html, dynamic data, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
public virtual JsonResult ComboWireJson(bool success, string html, dynamic data/*, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet*/)
{
return Json(new ComboWireJson(success, html, data), behavior);
return Json(new ComboWireJson(success, html, data)/*, behavior*/);
}

/// <summary>
Expand Down Expand Up @@ -164,7 +165,7 @@ public virtual string RenderView(string viewName, object model, bool enableFormC
/// Called before the action method is invoked.
/// </summary>
/// <param name="filterContext">Information about the current request and action.</param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
UpdateModelStateErrors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using BetterModules.Core.Environment.Assemblies;
using Microsoft.AspNet.Mvc;

namespace BetterModules.Core.Web.Mvc.Extensions
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public DefaultControllerExtensions(IAssemblyLoader assemblyLoader)
/// </returns>
public virtual bool IsControllerType(Type type)
{
return IsControllerType<IController>(type);
return IsControllerType<Controller>(type);
}

/// <summary>
Expand Down Expand Up @@ -76,7 +76,7 @@ public string GetControllerName(Type controllerType)
/// <returns>Controller types from assembly.</returns>
public IEnumerable<Type> GetControllerTypes(Assembly assembly)
{
return GetControllerTypes<IController>(assembly);
return GetControllerTypes<Controller>(assembly);
}

/// <summary>
Expand Down Expand Up @@ -129,7 +129,7 @@ public IEnumerable<MethodInfo> GetControllerActions(Type controllerType)
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
/// <returns>Controller action names.</returns>
public IEnumerable<MethodInfo> GetControllerActions<TController>() where TController : ControllerBase
public IEnumerable<MethodInfo> GetControllerActions<TController>() where TController : Controller
{
var controllerType = typeof(TController);
return GetControllerActions(controllerType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.AspNet.Mvc;

namespace BetterModules.Core.Web.Mvc.Extensions
{
Expand Down Expand Up @@ -65,6 +65,6 @@ public interface IControllerExtensions
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
/// <returns>Controller action names.</returns>
IEnumerable<MethodInfo> GetControllerActions<TController>() where TController : ControllerBase;
IEnumerable<MethodInfo> GetControllerActions<TController>() where TController : Controller;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.IO;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;

namespace BetterModules.Core.Web.Mvc.Extensions
{
Expand All @@ -15,28 +18,32 @@ public static class ViewRenderingExtensions
/// <returns>View, rendered to string</returns>
public static string RenderViewToString(this CoreControllerBase controller, string viewName, object model, bool enableFormContext = false)
{
if (string.IsNullOrEmpty(viewName) || viewName.ToLower() == controller.ControllerContext.RouteData.GetRequiredString("action").ToLower())
var services = controller.ActionContext.HttpContext.RequestServices;
var compositeViewEngine = services.GetRequiredService<ICompositeViewEngine>();
var htmlHelperOptions = services.GetRequiredService<IOptions<MvcViewOptions>>().Options.HtmlHelperOptions;

if (string.IsNullOrEmpty(viewName) || viewName.ToLower() == controller.ActionContext.ActionDescriptor.Name.ToLower())
{
var areaName = controller.ControllerContext.RouteData.GetRequiredString("area");
var controllerName = controller.ControllerContext.RouteData.GetRequiredString("controller");
var actionName = controller.ControllerContext.RouteData.GetRequiredString("action");
var areaName = controller.ActionContext.RouteData.Values["area"];
var controllerName = controller.ActionContext.RouteData.Values["controller"];
var actionName = controller.ActionContext.ActionDescriptor.Name;

viewName = string.Format("~/Areas/{0}/Views/{1}/{2}.cshtml", areaName, controllerName, actionName);
viewName = $"~/Areas/{areaName}/Views/{controllerName}/{actionName}.cshtml";
}

controller.ViewData.Model = model;

using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
var viewResult = compositeViewEngine.FindPartialView(controller.ActionContext, viewName);
var viewContext = new ViewContext(controller.ActionContext, viewResult.View, controller.ViewData, controller.TempData, sw, htmlHelperOptions);
if (enableFormContext && viewContext.FormContext == null)
{
viewContext.FormContext = new FormContext();
}

viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
viewResult.View.RenderAsync(viewContext);
//viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);

return sw.GetStringBuilder().ToString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Web.Routing;
using Microsoft.Web.Mvc;
using Microsoft.AspNet.Routing;

namespace BetterModules.Core.Web.Mvc.Routes
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Security.Principal;
using BetterModules.Core.Security;
using BetterModules.Core.Web.Web;
using Microsoft.AspNet.Http;

namespace BetterModules.Core.Web.Security
{
Expand Down Expand Up @@ -28,7 +28,7 @@ public DefaultWebPrincipalProvider(IHttpContextAccessor httpContextAccessor)
/// </returns>
public override IPrincipal GetCurrentPrincipal()
{
var currentHttpContext = httpContextAccessor.GetCurrent();
var currentHttpContext = httpContextAccessor.HttpContext;

if (currentHttpContext == null)
{
Expand Down
Loading

0 comments on commit 41b8d50

Please sign in to comment.