/// <summary> /// /// </summary> /// <param name="controller"></param> /// <param name="module"></param> /// <param name="file"></param> /// <returns></returns> public static async Task <IActionResult> SpaAsync(this Controller controller, string module, string file) { var env = controller.ControllerContext.HttpContext.RequestServices.GetService <IHostEnvironment>(); var moduleName = module ?? controller.GetType().Assembly.GetName().Name; if (env.IsDevelopment() && SeedSpaBuilderExtensions.UrlHash.ContainsKey(moduleName)) { var baseUriTaskFactory = SeedSpaBuilderExtensions.UrlHash[moduleName] as Func <Task <Uri> >; var request = (HttpWebRequest)WebRequest.Create($"{await baseUriTaskFactory()}{moduleName}/{file}"); request.Method = controller.Request.Method; request.ContentType = controller.Request.ContentType; using (var response = (HttpWebResponse)request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { var responseReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet)); var responseText = responseReader.ReadToEnd(); return(await Task.FromResult(controller.Content(responseText, response.ContentType))); } } } return(await Task.FromResult(controller.View($"~/Areas/{moduleName}/ClientApp/dist/{file}"))); }
protected void ProtectsFromOverpostingId(Controller controller, String postMethod) { MethodInfo methodInfo = controller .GetType() .GetMethods() .First(method => method.Name == postMethod && method.IsDefined(typeof(HttpPostAttribute), false)); Assert.NotNull(methodInfo.GetParameters()[0].IsDefined(typeof(BindExcludeIdAttribute), false)); }
public static ActionResult GetODataServiceDocumentJsonV4( this Microsoft.AspNetCore.Mvc.Controller ctrl, TableSpec[] tables, Func <ActionResult> MetadataAction) { var mi = MetadataAction.GetMethodInfo(); var httpAttr = Attribute.GetCustomAttribute(mi, typeof(HttpGetAttribute)) as HttpGetAttribute; if (httpAttr == null) { throw new InvalidOperationException($"Action {mi.Name} must have [HttpGet] attribute"); } if (string.IsNullOrWhiteSpace(httpAttr.Template)) { throw new InvalidOperationException($"Action {mi.Name} don't have template in [HttpGet(<template>)] atttribute."); } var url = httpAttr.Template .Replace("[controller]", ctrl.GetType().Name.Replace("Controller", "")) .Replace("[action]", mi.Name); return(GetODataServiceDocumentJsonV4(ctrl, tables, url)); }
public static ViewResult GetView(this Microsoft.AspNetCore.Mvc.Controller ctrl, [CallerMemberName] string name = "", object model = null) { name = GetLastMethodName(name); if (InvocationHub.IsModuleInDebugMode()) { return(ctrl.View(name, model)); } else { var mdlName = ctrl.ControllerContext.HttpContext.Items[Consts.CONTEXT_ITEM_KEY_THEME_MODULE_NAME].ToString(); var controllerName = ctrl.GetType().Name.Replace("Controller", "", StringComparison.OrdinalIgnoreCase); var path = $"{Consts.MODULES_BASE_PATH}\\{mdlName}\\Views\\{controllerName}\\"; var file = ""; try { file = Directory.GetFiles(path, $"{name}.*").Single(); } catch (FileNotFoundException) { throw new ViewFileNotFoundException(mdlName, controllerName, name); } return(ctrl.View($"~/{path.Replace("\\","/")}{new FileInfo(file).Name}", model)); } }